我使用ajax创建搜索,每次在MVC中都会出错 [英] I used ajax to create search and I get error every time in MVC

查看:73
本文介绍了我使用ajax创建搜索,每次在MVC中都会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码ajax

this is my code ajax

$("#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);
                        }
                    })
                });





这是我的C#代码:



and this is my C# Code:

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);
        }





和这个html: -



and this 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>





我的尝试:



i想要使用ajax创建搜索



What I have tried:

i want create search using ajax

推荐答案

(#btnSearch)。click(function(){
var goverId = parseInt(
("#btnSearch").click(function () { var goverId = parseInt(


(#SellerSearch_gover_id)。val());
var cityId = parseInt(
("#SellerSearch_gover_id").val()); var cityId = parseInt(


(#SellerSearch_city_id)。val())|| 0;
var setData =
("#SellerSearch_city_id").val()) || 0; var setData =


这篇关于我使用ajax创建搜索,每次在MVC中都会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆