Partial view not working on ajax call mvc5

i am working on view which is actually create view for customer.

On "Create" View, I am using textbox to search the items from db and then display their detail on _part(Partial View)...The partial view action is correctly getting string and processing request except displaying partial view data on create view....

Create View:

<form>
    <input type="text" id="enginNo" />
    <input type="button" value="search" id="btnSearch" />
</form>
<div id="info" class="col-md-10">
<script type="text/javascript">
    $(document).ready(function () {
        alert("hola");
        $("#btnSearch").click(function () {
            alert("hola2");
            var enginNo = $('#enginNo').val();
            $.ajax(
                {
                    alert:("ajax called"),
                    type: 'Get',
                    data: { enginNo: enginNo },
                    url: '@Url.Action("CheckRecord")',
                    success: function (result) {
                        $('#result').html(result);
                    }
                });
        });
    });
</script>

Returning string "enginNo" to partial view action below:

public PartialViewResult CheckRecord(string enginNo)
{
    ViewBag.records = db.StockDMs.Where(ve => ve.EngineNumber == enginNo);
    if (Request.IsAjaxRequest())
    {
        return PartialView("_part", ViewBag.records);

    }
    else
        return null;
}

PartialView:

@model SM.CRM.AutosLoan.Models.Core.DomainModels.StockDM

<div>
    @{
        Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/SMClient.cshtml";
    }
    @foreach (var item in ViewBag.records)
     {
     <dd>
                @item.AutoCompanyBrand.Name
            </dd>

            <dd>
                @item.SMClientBranch.Name
            </dd>
     }

I don't know what i am doing wrong, thought if somebody help..Thanks for your time:-)


您的PartialViewResult正在返回html,因此请在您的ajax请求中给出dataType:'HTML' ,如下所示: -

$.ajax({
      type: 'Get',
      data: { enginNo: enginNo },
      url: '@Url.Action("CheckRecord")',
      dataType: 'HTML',  // add this line
      success: function (result) {
             $('#result').html(result);
      }
 });
链接地址: http://www.djcxy.com/p/42104.html

上一篇: 返回部分视图和消息

下一篇: 部分视图无法在ajax上调用mvc5