0

嗨,我的 MVC 应用程序有问题。我想点击 ActionLink > 从服务器加载数据 > 并将数据插入到同一页面中的 div 中。但是我的代码仍然在新页面上加载内容,我不知道为什么。有人帮我吗?

这是我在 HomeController 中的 Index.cshtml,

    @model IEnumerable<Kango_kmen.KangoKmenWS.WSEntityInfo>
    @Content.Script("jquery-1.9.1.min.js", Url)
    @Content.Script("jquery.unobtrusive-ajax.min.js",Url)
    @Content.Script("jquery.unobtrusive-ajax.js",Url)
    @Content.Script("myScript.js",Url)

    @section Entities{


    <table>
    <tr>
    <th>
      <input type="submit" value="zobrazit" />
    </th>
    <th>
        iD
    </th>
    <th>
        name
    </th>

    </tr>

    @foreach (var item in Model) {
    <tr>
    <td>
    @Ajax.ActionLink("..", "CheckEntity", new { id = item.iD }, 
            new AjaxOptions{ UpdateTargetId="properties",
                             HttpMethod="GET",
                             InsertionMode=InsertionMode.InsertAfter,
            })
    </td>
    <td>            
        @Html.DisplayFor(modelItem => item.iD)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.name)
    </td>

    </tr>  
    <tr>
     <td>
         <div id="properties">

         </div>
     </td>
    </tr>
     }
    </table>
     }


     @section PropertyInfo{
     <p>Property info</p>
     } 

     @section Properties{
     <p>properties</p>
     }

这是我在 HomeController 中的控制器

    public PartialViewResult CheckEntity(int id)
    {

        var model = WSConnect.getEntityInfo(id);
        if(model !=null){
            return PartialView("CheckEntity", model.property);
        }
        var modell = new WSEntityInfo[0];
        return PartialView("CheckEntity", modell);
    }

ChecktEntity 是部分视图,但每次我单击 ActionLink 控制器加载 url:/Home/CheckEntity/1000 以查看此新页面上的视图数据时“(

4

1 回答 1

2

您的代码有两点错误:

  1. 您已经包含了jquery.unobtrusive-ajax.js两次脚本,一次是缩小版,一次是标准版。你应该只包含一次
  2. jquery.unobtrusive-ajax.js不兼容,with jquery 1.9.1因为 jQuery 1.9 中的重大更改之一是该.live()方法已被删除并且jquery.unobtrusive-ajax.js脚本依赖于它。如果您查看您的 javascript 控制台,您会看到以下错误:The .live method is undefined.. 因此,如果您想在 ASP.NET MVC 中使用不显眼的 AJAX 功能,您将不得不降级 jQuery 的版本。例如1.8.3版本应该没问题。

所以:

@model IEnumerable<Kango_kmen.KangoKmenWS.WSEntityInfo>
@Content.Script("jquery-1.8.3.min.js", Url)
@Content.Script("jquery.unobtrusive-ajax.min.js", Url)
@Content.Script("myScript.js", Url)

还请学习如何调试您的 javascript 代码。使用诸如 FireBug 或 Chrome 开发人员工具栏之类的工具,所有这些错误都会在其中显示。

于 2013-02-17T18:13:19.010 回答