我的問題是,我正在研究ASP .NET MVC Ajax項目。在我將集合傳遞給視圖並使用foreach循環迭代此集合的情況下,我想在同一視圖上使用@Ajax.ActionLink()
添加新記錄,表單將以使用jQuery UI的對話框的形式顯示。但我不知道該怎麼做,因爲在IEnumerable
形式的模型對象,我無法通過模型屬性@Html.EditorFor()
方法,錯誤的是:如何將IEnumerable集合轉換爲使用MVC .NET的單個對象
CS1061:「IEnumerable的<國家>」不不包含「國家或地區名稱」和沒有擴展方法「國家或地區名稱」接受類型的第一個參數定義「的IEnumerable <國家>」可以找到(是否缺少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>
是一個集合,但我想在相同的視圖中創建窗體,但上面的錯誤正在發生。
的代碼示例,在這裏開始好辦法。 –
顯示生成此錯誤的代碼。編輯您的問題並添加該代碼。 – user3185569
@Janis我已添加我的代碼,請幫助 – Savvy