I have an application developed on ASP.NET MVC5. Following is the code of the controller:
namespace ACCOUNTS2.Controllers
{
public class SelectDealController : Controller
{
private ACCOUNTSEntities db = new ACCOUNTSEntities();
// GET: /SelectDeal/
public ActionResult Index(string option,string search)
{
var sell_deal = db.Sell_Deal.Include(s => s.Client_Info);
return View(sell_deal.ToList());
}
[HttpPost]
public JsonResult GetSearchingData(string SearchBy, string SearchValue)
{
List<Sell_Deal> selldeal = new List<Sell_Deal>();
if(SearchBy == "Dealer_Name")
{
string Dealer_Name = SearchValue;
selldeal = db.Sell_Deal.Where(x => x.Dealer_Name.StartsWith(SearchValue) || SearchValue == null).ToList();
return Json(selldeal, JsonRequestBehavior.AllowGet);
}
else if(SearchBy == "Location")
{
selldeal = db.Sell_Deal.Where(x => x.Location.StartsWith(SearchValue) || SearchValue == null).ToList();
return Json(selldeal, JsonRequestBehavior.AllowGet);
}
else if (SearchBy == "Client_Name")
{
selldeal = db.Sell_Deal.Where(x => x.Client_Info.Client_Name.StartsWith(SearchValue) || SearchValue == null).ToList();
return Json(selldeal, JsonRequestBehavior.AllowGet);
}
else
{
var sell_deal1 = db.Sell_Deal.Include(s => s.Client_Info).ToList();
return Json(sell_deal1, JsonRequestBehavior.AllowGet);
}
}
Following is the code of my model:
namespace ACCOUNTS2
{
using System;
using System.Collections.Generic;
public partial class Sell_Deal
{
public Sell_Deal()
{
this.Sell_Deal_Details = new HashSet<Sell_Deal_Details>();
}
public decimal Deal_ID { get; set; }
public Nullable<System.DateTime> Deal_Date { get; set; }
public Nullable<decimal> Total_Amount_Remaining { get; set; }
public Nullable<decimal> Client_ID { get; set; }
public string Dealer_Name { get; set; }
public Nullable<System.DateTime> Validity_Date { get; set; }
public string Location { get; set; }
public Nullable<decimal> Deal_Amount { get; set; }
public virtual Client_Info Client_Info { get; set; }
public virtual ICollection<Sell_Deal_Details> Sell_Deal_Details { get; set; }
}
}
Following is my jquery code in the view:
$(document).ready(function () {
$("#BtnSearch").click(function () {
var SearchBy = $("#SearchBy").val();
var SearchValue = $("#Search").val();
var SetData = $("#DataSearching");
SetData.html("");
$.ajax({
type: "post",
url: "/SelectDeal/GetSearchingData?SearchBy=" + SearchBy + "&SearchValue=" + SearchValue,
datatype: JSON,
contentType: "html",
success: function (result) {
if (result.length == 0) {
SetData.append('<tr style="color:red"><td colspan="3">No Match Data</td></tr>')
}
else {
$.each(result, function (Index, value) {
var val = "<tr>" +
"<td>" + value.Deal_Date + "</td>" +
"<td>" + value.Total_Amount_Remaining + "</td>" +
"<td>" + value.Dealer_Name + "</td>" +
"<td>" + value.Validity_Date + "</td>" +
"<td>" + value.Location + "</td>" +
"<td>" + value.Deal_Amount + "</td>" +
"<td>" + value.Client_Info.Client_Name + "</td>" +
"</tr>";
SetData.append(val)
});
}
}
});
});
});
When I click the button, I expect the filtered data is shown in the table according to the logic written in the JsonResult method in the controller, but it is not happening. As far as I have noticed, the first "if" condition on the ajax success event is appending data to the element but It doesn't work at all in the "else" body of the ajax code. Have I done anything wrong in my code? Do I need to make any correction here? Please help!