【发布时间】:2016-08-12 14:56:48
【问题描述】:
我的问题是,我正在开发 ASP .NET MVC Ajax 项目。在其中我将集合传递给视图并使用 foreach 循环迭代此集合,然后我想在同一视图上使用 @Ajax.ActionLink() 添加新记录,表单将采用 jQuery UI 对话框的形式。但是我不知道该怎么做,因为IEnumerable形式的模型对象我无法将模型属性传递给@Html.EditorFor()方法,错误是:
CS1061:“IEnumerable
”不包含“CountryName”的定义,并且找不到接受“IEnumerable ”类型的第一个参数的扩展方法“CountryName”(您是否缺少 using 指令或程序集参考?)
查看.cshtml代码
@model IEnumerable<UBSAjax.Models.Country>
....
<style>
.deleteButton {
display:none !important;
}
</style>
<h2>Counties</h2>
<table id="myTable">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.CountryName)</th>
<th>@Html.DisplayNameFor(model => model.CountryCode)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.CountryName)</td>
<td>@Html.DisplayFor(modelItem => item.CountryCode)</td>
<td class="deleteButton">
@Html.ActionLink("Edit", "Edit", new { id = item.CountryId }) |
@Html.ActionLink("Details", "Details", new { id = item.CountryId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CountryId })
</td>
</tr>
}
</tbody>
</table>
<!--Ajax Assets-->
@{
AjaxOptions options = new AjaxOptions();
options.InsertionMode = InsertionMode.InsertAfter;
options.HttpMethod = "GET";
//options.LoadingElementId = "imgloader";
//options.LoadingElementDuration = 1000;
options.Confirm = "Do you want to get form ?????";
options.OnFailure = "failureFunc";
options.OnSuccess = "successFunc";
//options.OnBegin = "beginFunc";
//options.OnComplete = "completeFunc";
options.UpdateTargetId = "CountryContentId";
}
<div id="dialog" title="Login Form">
@using (Ajax.BeginForm("Create", "Country", options))
{
<div class="form-group">
@Html.EditorFor(model => model.CountryName, new { @class = "form-control", @placeholder = "Enter Country Name" })
@Html.ValidationMessageFor(model => model.CountryName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.EditorFor(model => model.CountryCode, new { @class = "form-control", @placeholder = "Enter Country Code" })
@Html.ValidationMessageFor(model => model.CountryCode, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<input type="submit" value="Add Country" class="btn btn-success" />
</div>
}
</div>
@model IEnumerable<UBSAjax.Models.Country> 是一个集合,但我想在同一个视图中创建表单,但出现上述错误。
【问题讨论】:
-
代码示例是从这里开始的好方法。
-
显示产生此错误的代码。编辑您的问题并添加该代码。
-
@Janis 我已经添加了我的代码,请帮忙
-
@user3185569 我已经添加了我的代码,请帮忙
-
使用具有新
Country属性和集合属性的视图模型。或使用@Html.Action()调用服务器方法,该方法返回表单的部分视图以获取新的Country
标签: c# asp.net-mvc