【问题标题】:Rendering a partial View with Ajax Unobtrusive使用 Ajax Unobtrusive 渲染局部视图
【发布时间】:2016-01-03 03:14:35
【问题描述】:

我有以下视图和部分视图。在索引操作方法中,我需要将 Create 呈现为部分视图。根据this 问题中的答案,我尝试使用 Ajax 辅助方法(我发现 AJAX 和 JS 对我来说很难理解)。

@model IEnumerable<TEDALS_Ver01.Models.UserRight>

@{
ViewBag.Title = "Index";
}
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.UserName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.UserCode)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IsReader)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IsEditor)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IsExporter)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IsAdmin)
    </th>
    <th></th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UserCode)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsReader)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsEditor)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsExporter)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsAdmin)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.UserRightID }) |
        @Html.ActionLink("Details", "Details", new { id=item.UserRightID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.UserRightID })
    </td>
</tr>
}

</table>
<p>
@Ajax.ActionLink("Creates", "Creates", "UserRights", new { area = "Creates" }, new AjaxOptions { UpdateTargetId="Creates"})
</p>
<div id="Creates">
@Html.RenderPartial("_Creates",Model);
</div>

局部视图

  @model TEDALS_Ver01.Models.UserRight
  @Scripts.Render("~/bundles/jqueryval")
  @using (Ajax.BeginForm("Creates", "Creates", new AjaxOptions { HttpMethod = "Post", InsertionMode = InsertionMode.Replace }))
 {
 @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })

    @Html.LabelFor(model => model.UserCode, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.UserCode, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.UserCode, "", new { @class = "text-danger" })

    @Html.LabelFor(model => model.IsReader, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.IsReader)
    @Html.ValidationMessageFor(model => model.IsReader, "", new { @class = "text-danger" })

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
   </div>
  }

<div>
@Html.ActionLink("Back to List", "Index")
</div>

执行时出错:

 Compilation Error
 Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

 Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

任何帮助将不胜感激。我已尝试以多种方式更改 RenderPartial 的参数,但均未成功。

评论

我已在布局中添加了所有脚本。

错误

 An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

 Additional information: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[TEDALS_Ver01.Models.UserRight]', but this dictionary requires a model item of type 'TEDALS_Ver01.Models.UserRight'.

根据 Greg 的输入进行编辑

主视图

   @model IEnumerable<TEDALS_Ver01.Models.UserRight>
   @using TEDALS_Ver01.Models
   <p>
   @Html.ActionLink("Create New", "Create")
   </p>
   <table class="table">
   <tr>
   <th>
        @Html.DisplayNameFor(model => model.UserName)
   </th>
   <th>
        @Html.DisplayNameFor(model => model.UserCode)
   </th>
   <th>
        @Html.DisplayNameFor(model => model.IsReader)
   </th>
   <th>
        @Html.DisplayNameFor(model => model.IsEditor)
   </th>
   <th>
        @Html.DisplayNameFor(model => model.IsExporter)
   </th>
   <th>
        @Html.DisplayNameFor(model => model.IsAdmin)
   </th>
   <th></th>
  </tr>
  @foreach (var item in Model) {
  <tr>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UserCode)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsReader)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsEditor)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsExporter)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsAdmin)
    </td>

    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.UserRightID }) |
        @Html.ActionLink("Details", "Details", new { id=item.UserRightID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.UserRightID })
    </td>
   </tr>
   }

   </table>

   <input type="button" value="Create" id="Creates" />
   <div id="Creates">
   @{  
   Html.Partial("_Creates",new TEDALS_Ver01.Models.UserRight());
   }
   </div>

我了解一些应该用于正确处理的脚本丢失了。但我不知道应该在哪里添加脚本。我之前也遇到过同样的问题,无法解决我排除了使用脚本的部分。

“我的创建”按钮不执行任何操作。在浏览器控制台中,我收到错误 Element not found。

【问题讨论】:

    标签: c# ajax asp.net-mvc-5 asp.net-mvc-partialview unobtrusive-ajax


    【解决方案1】:

    您收到状态的错误消息

    传入字典的模型项属于类型 'System.Collections.Generic.List`1[TEDALS_Ver01.Models.UserRight]', 但是这本字典需要类型的模型项 'TEDALS_Ver01.Models.UserRight'

    这是说您创建的视图只处理一个UserRight,但您给了它一个List&lt;UserRight&gt;。该错误似乎发生在您的主视图中:

    <div id="Creates">
    @Html.RenderPartial("_Creates",Model);
    </div>
    

    当前模型是一个集合,因此使用该对象渲染 _Creates.cshtml 将失败。

    通常,解决这个问题的方法是遍历模型

     @foreach(var modelItem in Model) {
      <div id="Creates">
        @Html.RenderPartial("_Creates", modelItem);
      </div>
     }
    

    但在这种情况下,我认为这根本不是你想要的

    现在,如果您删除了对 RenderPartial 的调用,它可能会编译,并且当您单击 Creates 链接时,它会向 Creates 操作发送一个 GET,该操作将呈现它连接到的任何视图(这听起来像您在您的问题)。

    然后用户填写部分表单并将其发布到 Creates,然后 div#Creates 再次被替换为来自发布操作的任何视图。

    听起来您真正想要的是呈现表单,然后在发布时替换表单。为此,您首先必须删除@Ajax.ActionLink,因为您不希望在页面加载时将div#Creates 替换为链接。你在正确的轨道上渲染那个部分,但问题是你给它的对象。

    你可以尝试给它一个空的 UserRight

    <div id="Creates">
      @Html.Partial("_Creates", new TEDALS_Ver01.Models.UserRight());
    </div>
    

    但如果这不起作用,您需要为该视图创建一个 ViewModel,其中包含您要显示的 UserRights 列表以及您的表单可以使用的模板 UserRight。


    附带说明,因为我没有足够的分数来评论@dylan-slabbinck 的答案,所以您想使用@Html.Partial,而不是@Html.RenderPartial。

    【讨论】:

    • public ActionResult _Creates() { return PartialView("_Creates",new UserRight()); }
    • 这是我用于创建的控制器操作方法。
    • 我已经用你的 cmets 的代码更新了这个问题。我没有让“创建”按钮工作。
    • 我错过了按钮中的函数调用。我已经正确调用了函数。只是想把它放在我的视图中。
    【解决方案2】:

    像这样使用@{ Html.RenderPartial("_Creates", Model);}

    Html.Render... helpers return void,这可能是您收到错误的原因

    【讨论】:

    • 在这种情况下,我应该在我的控制器返回部分视图中有一个等效参数吗?
    • 如果我将函数命名为 _Create 和 Create 有什么区别吗? _ 暗示什么?
    • _Creates 是视图/部分视图的名称
    • 我是问下划线的使用有什么意义,还是说它是局部视图?
    • 据我所知,命名约定是部分内容通常以_ 开头 - 但归根结底取决于开发它的人!
    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2011-04-08
    • 2023-03-30
    相关资源
    最近更新 更多