我有以下视图和部分视图。在索引操作方法中,我需要将 Create 呈现为部分视图。根据这个问题的答案,我尝试使用 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。