【问题标题】:How to convert IEnumerable collection into single object using MVC .NET如何使用 MVC .NET 将 IEnumerable 集合转换为单个对象
【发布时间】: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&lt;UBSAjax.Models.Country&gt; 是一个集合,但我想在同一个视图中创建表单,但出现上述错误。

【问题讨论】:

  • 代码示例是从这里开始的好方法。
  • 显示产生此错误的代码。编辑您的问题并添加该代码。
  • @Janis 我已经添加了我的代码,请帮忙
  • @user3185569 我已经添加了我的代码,请帮忙
  • 使用具有新Country 属性和集合属性的视图模型。或使用@Html.Action() 调用服务器方法,该方法返回表单的部分视图以获取新的Country

标签: c# asp.net-mvc


【解决方案1】:

改为使用视图模型,创建一个包含国家集合和单个新国家数据的视图模型,从那里将视图分别分成显示和编辑器模板

【讨论】:

  • 我只想强制转换集合,即 var obj = (List),就像那样,因为我将集合返回到视图并且在视图中我想将其转换为单个对象
  • 如何创建视图模型,它将同时保存国家集合和一个新的国家数据,请给我任何示例,谢谢
  • 输入代码不方便,基本上一个视图模型只是一个像你的模型一样的 poco 类,除了你不在上下文中添加它的 dbset,创建一个名为 countryvm 或你喜欢的,并让它拥有的属性IEnumerable 国家和另一个拥有单个国家数据的国家,将其命名为 NewCountry 或什么,从这里使用您制作的新虚拟机更改您当前视图的模型,将整个表格移动到国家列表的显示模板中(google on如何创建自定义编辑器和显示模板)和使用编辑器的表单。最后更新你的控制器以使用虚拟机
猜你喜欢
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
相关资源
最近更新 更多