1

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!

Uzair Raza
  • 55
  • 4

1 Answers1

0

Your Ajax request is invalid. Remove contentType: "html" and modify datatype: JSON to datatype: "json";

$.ajax({
    type: "post",
    url: "/SelectDeal/GetSearchingData?SearchBy=" + SearchBy + "&SearchValue=" + SearchValue,
    datatype: "json",
    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)
            });
        }
    }
});

Also, you are applying Post request but the parameters SearchBy and SearchValue are passed to serverside via GET. It is not proper way to perform it. Post the parameters in the request body not in the url.

lucky
  • 12,734
  • 4
  • 24
  • 46
  • Thank you so much for the response. What do you mean by ' Post the parameters in the request body not in the url' ? How do I post post the request body in the URL? – Uzair Raza Jan 02 '18 at 05:32
  • Well, you can't post anything in the url. You could create an object from "SearchBy" and "SearchValue" parameters, serialize to json send it and send it to serverside. And you should create a model class to handle this request. There is a very good explanation. https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api – lucky Jan 02 '18 at 05:39
  • "SearchBy" and "SearchValue" parameters are created and set in the view when the button is clicked and then sent to the Controller's JsonResult method for processing. Where exactly should I create the objects from "SearchBy" and "SearchValue" parameters? I am sort of perplexed. Where exactly do I have to make changes in my application? And also, where should I serialize the parameters in Json? – Uzair Raza Jan 02 '18 at 05:52
  • You should serialize the object to json client side. You can review this article. https://stackoverflow.com/questions/48020084/error-while-calling-webapi-in-my-asp-net-project/48020131#48020131 – lucky Jan 02 '18 at 05:55