简体   繁体   English

使用Ajax Unobtrusive渲染局部视图

[英]Rendering a partial View with Ajax Unobtrusive

I have the following View and Partial View. 我有以下视图和局部视图。 In the index action method I need the Create to rendered as a Partial View. 在索引操作方法中,我需要将Create呈现为部分视图。 Based on the answer in this question I tried using Ajax helper methods(I find AJAX and JS so hard for me to comprehend). 根据这个问题的答案,我尝试使用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>

Partial View 部分视图

  @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>

I get error while executing : 执行时出现错误:

 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

Any help would be appreciated. 任何帮助,将不胜感激。 I have tried changing the arguments for RenderPartial in many ways and found no success. 我试图以多种方式更改RenderPartial的参数,但没有成功。

Comments 评论

I have added all the scripts in my Layout. 我已经在布局中添加了所有脚本。

Error 错误

 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'.

EDIT based on the inputs from Greg 根据Greg的输入进行编辑

Main View 主视图

   @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>

I understand some scripts that should be used to get it right is missing. 我了解缺少一些应该用来正确处理的脚本。 But I don not know where the scripts should be added. 但是我不知道应该在哪里添加脚本。 I have come across the same problem before and unable to resolve I excluded the part that used scripts. 我之前遇到过相同的问题,无法解决,所以我排除了使用脚本的部分。

My Create button doesn't do anything. 我的创建按钮没有任何作用。 In the Browser console I get the error Element not found. 在浏览器控制台中,我收到错误“找不到元素”。

The error message that you get states 您得到的错误消息状态

The model item passed into the dictionary is of type 'System.Collections.Generic. 传递到字典中的模型项的类型为 'System.Collections.Generic。 List `1[TEDALS_Ver01.Models.UserRight]', but this dictionary requires a model item of type 'TEDALS_Ver01.Models. 列出 “ 1 [TEDALS_Ver01.Models.UserRight]”,但此字典需要类型为“ TEDALS_Ver01.Models”的模型项。 UserRight ' 用户权限 '

This is saying that the view you created only handles one UserRight but you gave it a List<UserRight> . 这就是说,您创建的视图仅处理一个UserRight但是您给了它一个List<UserRight> The error seems to happen here in your main view: 该错误似乎在您的主视图中发生在这里:

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

The current Model is a collection so rendering _Creates.cshtml with a that object will fail. 当前模型是一个集合,因此使用那个对象渲染_Creates.cshtml将失败。

Normally, the way around this is to loop over model 通常,解决此问题的方法是遍历模型

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

But in this case, I don't think it's what you want at all . 但是在这种情况下, 我根本不是您想要的

Right now if you deleted the call to RenderPartial it will likely compile and when you click the Creates link it will send a GET to the Creates action which will render whatever view its wired to (which sounds like the partial you posted in your question). 现在,如果您删除了对RenderPartial的调用,它可能会编译,并且当您单击Creates链接时,它将向Creates操作发送一个GET,该操作将呈现其连接的任何视图(听起来像是您在问题中发布的局部视图)。

The user then fills out the form in the partial and POSTs it to Creates and the div#Creates is then replaced again with whatever view comes from the post action. 然后,用户在部分表单中填写表格,并将其过帐到“创建”中,然后再次用来自该过帐操作的任何视图替换div#Creates

It sounds like what you really want is to render the form and then replace the form when it's POSTed. 听起来您真正想要的是呈现表单,然后在发布表单时将其替换。 To do that you'll first have to remove the @Ajax.ActionLink since you don't want div#Creates to be replaced by a link when the page loads. 为此,您首先必须删除@Ajax.ActionLink因为您不希望在页面加载时将div#Creates替换为链接。 You're on the right track rendering that partial, but the problem is object you gave it. 您正在正确地渲染那部分,但是问题是您给它的对象。

You can try giving it an empty UserRight 您可以尝试给它一个空的UserRight

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

But if that doesn't work you'll need to create a ViewModel for that view that contains the list of UserRights you want displayed as well as a template UserRight that your form can use. 但是,如果这样不起作用,则需要为该视图创建一个ViewModel,其中包含要显示的UserRight列表以及表单可以使用的UserRight模板。


As a side note, because I don't have enough points to comment on @dylan-slabbinck answer, you want to use @Html.Partial, not @Html.RenderPartial. 附带说明一下,由于我没有足够的意见来评论@ dylan-slabbinck答案,因此您想使用@ Html.Partial,而不是@ Html.RenderPartial。

Use it like this @{ Html.RenderPartial("_Creates", Model);} @{ Html.RenderPartial("_Creates", Model);}

Html.Render... helpers return void , that's probably the reason you are getting the error Html.Render...助手返回void ,这可能是您收到错误的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM