Hi mahesh213,
Use the Replace method inside the FOR loop. Based on thr value check whether contains %, replace the Equal with Like operator.
I have updated your code. Change your code accordingly.
View
$scope.Save = function () {
var Id1 = $scope.items;
var rId = $scope.RName;
var details = {};
var array = [];
for (var i = 0; i < $scope.items.length; i++) {
var data = {};
data.Text = $scope.items[i].CName;
if ($scope.items[i].CType.toLowerCase() == 'dropdown') {
data.Value = $scope.items[i].FromTable.toLowerCase() == "all" ? "%" : $scope.items[i].FromTable;
} else if ($scope.items[i].CType.toLowerCase() == 'datepicker') {
data.Value = ConvertDate($scope.items[i].Date.toString());
} else if ($scope.items[i].CType.toLowerCase() == 'textbox') {
data.Value = $scope.items[i].ValueCName;
}
array.push(data)
details.RId = rId;
details.details = array;
}
$http({
method: "Post",
url: "/Home/PopulateTable",
dataType: 'json',
headers: { "Content-Type": "application/json" },
data: '{values: ' + JSON.stringify(details) + '}'
}).success(function (data) {
// Assign scope to bind gridview.
}).error(function (err) {
})
}
Controller
public JsonResult PopulateTable(Detail values)
{
TestEntities db = new TestEntities();
// Getting the query from Report1 table.
string query = db.Report1.Where(x => x.RId == values.RId).Select(x => x.RQuery).FirstOrDefault();
for (int i = 0; i < values.details.Count; i++)
{
string key = "{" + values.details[i].Text + "}";
string value = "'" + values.details[i].Value + "'";
// Replace the place holder with value.
if (values.details[i].Value == "%")
{
query = query.Replace("= " + key, "LIKE '%'");
query = query.Replace("=" + key, "LIKE '%'");
}
else
{
query = query.Replace(key, value);
}
}
// Execute your final query and return json result and bind the gridview on success function.
// Your code to return json.
return Json(null, JsonRequestBehavior.AllowGet);
}