为什么我的<div>不会更新?

时间:2015-11-30 00:34:19

标签: jquery asp.net-mvc-4

该应用程序最初旨在更新&#34; div-list&#34;阻止搜索,并在传递有效的产品代码以搜索 LINQ 时返回了有效的产品,但它未加载部分页面_ProductCategoryList。要测试我已将<p>updated</p>添加到js函数loadtodiv()中,但它也没有显示该消息。为什么这个部分页面或此html消息不会显示在该div上?

在我的MVC4剃须刀应用程序中,我有一个带有以下剃刀语法的索引页面:

@using(Ajax.BeginForm("GetCategory", "Home",
new AjaxOptions { 
    HttpMethod="GET", 
    UpdateTargetId="div-list",
    InsertionMode = InsertionMode.Replace,
    OnSuccess = "loadtodiv"
}))
{
    <input type="text" class="form-control" name="Code" value="" style="width:500px;" />
    <input type="Submit" class="btn btn-default" value="Search" id="search-btn"/>
}

<br />

<script type="text/javascript">
    function loadtodiv() {
        $('#div-list').html('<p>updated</p>');
    }
</script>


<div class="jumbotron">
    <h2>Index</h2>        


</div>
<div id="div-list">

<p>this is empty</p>    
</div>

索引页面的控制器是:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace eComm1.Controllers
    {
        public class HomeController : Controller
        {
            TUDBEntities _db = new TUDBEntities();

            //
            // GET: /Home/

            public ActionResult Index()
            {
                return View();
            }

            public ActionResult GetCategory(string Code)
            {
                var categories = _db.mt_ProductCategories.Where(pc => pc.CatCode == Code).FirstOrDefault();
                return PartialView("_ProductCategoryList", categories);
            }
    }
}

我的部分视图_ProductCategoryList.cshtml是:

@model IEnumerable<eComm1.Models.ProductCategory>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CategoryCode)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Url)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Url)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

1 个答案:

答案 0 :(得分:3)

GetCategory()方法中的查询会返回单个ProductCategory.FirstOrDefault()的使用,然后您将其传递给期望IEnumerable<ProductCategory>导致异常的部分视图。并且OnSuccess函数永远不会运行,因为没有成功。

更改您的查询以返回集合

public ActionResult GetCategory(string Code)
{
    var categories = _db.mt_ProductCategories.Where(pc => pc.CatCode == Code);
    return PartialView("_ProductCategoryList", categories);
}
相关问题