2

我有以下视图和部分视图。在索引操作方法中,我需要将Create创建为部分视图。基于this问题的答案,我尝试使用Ajax帮助器方法(我发现AJAX和JS很难让我理解)。使用Ajax渲染部分视图不显眼

@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的参数,但未找到任何成功。

评论

我在布局中添加的所有脚本。基于输入格雷格

主视图

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

错误

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

编辑我明白了一些脚本,应该用得到它的权利缺失。但我不知道应该在哪里添加脚本。我以前遇到同样的问题,无法解决我排除了使用脚本的部分。

我的创建按钮不会执行任何操作。在浏览器控制台中,我收到找不到错误的元素。

回答

3

错误消息的原因,你得到美国

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

这是说,你创建的视图只处理一个UserRight,但你给它一个List<UserRight>。错误似乎是你的主要观点在这里发生:

<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链接时,它会发送一个GET到Creates动作,它将呈现任何视图到它的连线(这听起来像你张贴的部分你的问题)。

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

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

你可以尝试给它一个空UserRight

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

但如果不工作,你需要创建一个视图模型为包含UserRights的列表视图要显示以及一个您的表单可以使用的模板UserRight。


作为一个方面说明,因为我没有足够的积分上@迪伦 - slabbinck答案发表评论,您要使用@ Html.Partial,不@ Html.RenderPartial。

+0

'public ActionResult _Creates() { return PartialView(“_ Creates”,new UserRight()); }' – Vini

+0

这是我创建的Controller操作方法。 – Vini

+0

我已经用您的意见与代码更新了问题。我没有得到创建按钮的工作。 – Vini

1

使用方法如下@{ Html.RenderPartial("_Creates", Model);}

Html.Render...助手返回void,这可能是你所得到的错误

+0

在这种情况下,我应该有我的控制器返回部分视图中的等效参数? – Vini

+0

我仍然有同样的错误, – Vini

+0

如果我命名函数_Create和Create,是否有任何区别? '_'暗示了什么? – Vini