Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code ajax
HTML
$("#btnSearch").click(function () {
                    var goverId = parseInt($("#SellerSearch_gover_id").val());
                    var cityId = parseInt($("#SellerSearch_city_id").val()) || 0;
                    var setData = $("#tableid");

                    setData.html("");
                    $.ajax({
                        type: "POST",
                        url: "/ReSeller/GetSearch?goverId" + goverId + "&cityId" + cityId,
                        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 Data = "<tr>" +
                                        "<td>" + value.seller.seller_name_en + "</td>" +
                                        "<td>" + value.seller.governorate.gover_name_en + "</td>" +
                                        "<td>" + value.seller.city.city_name_en + "</td>" +
                                        "<td>" + value.seller.address_en + "</td>" +
                                        "<td>" + value.seller.phone1 + "</td>" +
                                        "<td>" + value.seller.phone2 + "</td>" +
                                        "</tr>";

                                    setData.append(Data);
                                })
                            }
                        },
                        error: function (xhr, ajaxOptions, errorThrown) {
                            alert(xhr.responseText);
                        }
                    })
                });


and this is my C# Code:
C#
public JsonResult GetSearch(int goverId, int cityId)
        {
            SellerSearchViewModel model = new SellerSearchViewModel();

            try
            {
                var result = db.seller.Where(x => x.gover_id == goverId || x.city_id == cityId).ToList();
                model.seller = result;
            }
            catch(FormatException)
            {
                Console.WriteLine("Error");
            }
            return Json(model, JsonRequestBehavior.AllowGet);
        }


and this html :-
HTML
@model WepProjectWhiteWhale.Models.SellerSearchViewModel

@{
    ViewBag.Title = WhiteWhaleLanguage.ReSeller;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<style>
    table {
        table-layout: fixed;
        word-wrap: break-word;
    }

    .breadcrumb > li + li:before {
        color: #646464;
    }

    .Disable_ActiveClear {
        pointer-events: none;
        cursor: default;
    }
</style>

<div class="breadcrumbs">
    <div class="container">
        <ol class="breadcrumb breadcrumb1 animated wow slideInLeft" data-wow-delay=".5s" style="background-color: #b1b1b2;">
            <li><a href="@Url.Action("Index", "Home" )"><span class="glyphicon glyphicon-home" aria-hidden="true"></span>@WhiteWhaleLanguage.Home</a></li>
            <li class="active" style="color: #3f3f3f;">@WhiteWhaleLanguage.ReSeller</li>
        </ol>
    </div>
</div>

<div class="checkout">
    <div class="container" style="margin-bottom: 20px;">
        @*@using (Html.BeginForm("Index", "ReSeller", FormMethod.Post))
            {*@

        @if (UICulture == "ar")
        {
        @Html.DropDownListFor(model => model.SellerSearch.gover_id, (SelectList)ViewBag.GovernorateAr,
                                  WhiteWhaleLanguage.PleaseSelectYourGovernorate, new { @style = "padding:0 12px; margin-bottom: 15px; margin-left: 10px; width:40%;", @class = "form-control", required = "required", title = @WhiteWhaleLanguage.Required })
        @Html.ValidationMessageFor(model => model.SellerSearch.gover_id, "", new { @class = "text-danger" })
        }
        else
        {
        @Html.DropDownListFor(model => model.SellerSearch.gover_id, (SelectList)ViewBag.GovernorateEn,
                                 WhiteWhaleLanguage.PleaseSelectYourGovernorate, new { @style = "padding:0 12px; margin-bootom: 15px; margin-left: 10px; width:40%", @class = "form-control", required = "required", title = @WhiteWhaleLanguage.Required })
        @Html.ValidationMessageFor(model => model.SellerSearch.gover_id, "", new { @class = "text-danger" })
        }

        @if (UICulture == "ar")
        {
        @Html.DropDownListFor(model => model.SellerSearch.city_id, (SelectList)ViewBag.CityAr,
                                 WhiteWhaleLanguage.PleaseSelectYourCity, new { @style = "padding:0 12px; margin-bottom: 15px; margin-left: 10px; width:40%", @class = "form-control" })
        }
        else
        {
        @Html.DropDownListFor(model => model.SellerSearch.city_id, (SelectList)ViewBag.CityEn,
                                 WhiteWhaleLanguage.PleaseSelectYourCity, new { @style = "padding:0 12px; margin-bottom: 15px; margin-left: 10px; width:40%;", @class = "form-control" })
        }


        <input type="submit" id="btnSearch" name="" value=@WhiteWhaleLanguage.Search , class="btn btn-primary" style="width:180px;" />

        @* } *@
    </div>
    <div class="container">
        <div class="checkout-right animated wow slideInUp" data-wow-delay=".5s">
            <table class="timetable_sub">
                <thead>
                    <tr>
                        <th></th>
                        <th>
                            @WhiteWhaleLanguage.ReSellerName
                        </th>
                        <th>
                            @WhiteWhaleLanguage.gover_id
                        </th>
                        <th>
                            @WhiteWhaleLanguage.city_id
                        </th>
                        <th>
                            @WhiteWhaleLanguage.Address
                        </th>
                        <th>
                            @WhiteWhaleLanguage.phone1
                        </th>
                        <th>
                            @WhiteWhaleLanguage.phone2
                        </th>
                    </tr>
                </thead>
                <tbody id="tableid">
                    @{ var i = 0; }
                    @foreach (var item in Model.seller)
                    {
                        <tr class="rem1">
                            <td class="invert">@(i += 1)</td>
                            <td class="invert">
                                @if (UICulture == "ar")
                                {
                                    @Html.DisplayFor(modelItem => item.seller_name_ar)
                                }
                                else
                                {
                                    @Html.DisplayFor(modelItem => item.seller_name_en)
                                }
                            </td>
                            <td class="invert">
                                @if (UICulture == "ar")
                                {
                                    @Html.DisplayFor(modelItem => item.governorate.gover_name_ar)
                                }
                                else
                                {
                                    @Html.DisplayFor(modelItem => item.governorate.gover_name_en)
                                }
                            </td>
                            <td class="invert">
                                @if (UICulture == "ar")
                                {
                                    @Html.DisplayFor(modelItem => item.city.city_name_ar)
                                }
                                else
                                {
                                    @Html.DisplayFor(modelItem => item.city.city_name_en)
                                }
                            </td>
                            <td class="invert">
                                @if (UICulture == "ar")
                                {
                                    @Html.DisplayFor(modelItem => item.address_ar)
                                }
                                else
                                {
                                    @Html.DisplayFor(modelItem => item.address_en)
                                }
                            </td>
                            <td class="invert">
                                @Html.DisplayFor(modelItem => item.phone1)
                            </td>
                            <td class="invert">
                                @Html.DisplayFor(modelItem => item.phone2)
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>


What I have tried:

i want create search using ajax
Posted
Comments
Suvendu Shekhar Giri 10-Nov-17 7:57am    
What is the error you are getting?
MahmoudOmar 10-Nov-17 8:03am    
Generic failed
Laxmidhar tatwa technologies 10-Nov-17 10:21am    
UICulture is taken as @Viewbag.UICulture which is passed from controller with assigning value.
secondly in javascript call ajax in dropdown change event
thirdly each textbox give id and fill in the ajaxcall
massimiliano aronica 10-Nov-17 8:22am    
1. Decorate the action with HttpPost attribute
2. Add equal sign (=) in ajax url after parameters name
3. Change content-type to JSON
4. Set breakpoint in the action code to see if the ajax call now arrives.
F-ES Sitecore 10-Nov-17 8:37am    
You can't just dump all your code and expect people to help, you need to learn to step through your code and to debug. Does your javascript click event get called? Does the action get called? If not what does the network tab of your browser's dev tools say about the request? Which line throws the error? One thing I can see just looking at the code is that you're not forming a proper url

url: "/ReSeller/GetSearch?goverId" + goverId + "&cityId" + cityId,

if goverId is 1 and cityId is 2 your url will be

/ReSeller/GetSearch?goverId1&cityId2

that's not valid, it needs to be goverId=1 and cityId=2

url: "/ReSeller/GetSearch?goverId=" + goverId + "&cityId=" + cityId,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900