C# 批量编辑单个结果

C# 批量编辑单个结果,c#,asp.net-mvc,edit,C#,Asp.net Mvc,Edit,我得到了一个存储过程,我在视图中显示它以进行编辑。我制作了一个强类型的存储过程。当我编辑字段,然后按下保存按钮时,参数“cm”始终为空。它不显示列表,只显示一条记录 自定义模型: public class CustomModel { public string Description { get; set; } public System.Data.Linq.ISingleResult<GetItems_ListResult> ItemList

我得到了一个存储过程,我在视图中显示它以进行编辑。我制作了一个强类型的存储过程。当我编辑字段,然后按下保存按钮时,参数“cm”始终为空。它不显示列表,只显示一条记录

自定义模型:

public class CustomModel
    {
        public string Description { get; set; }
        public System.Data.Linq.ISingleResult<GetItems_ListResult> ItemList { get; set;}
    }
此控制器从视图接收数据:

 public ActionResult Details(int id)
 {
    var row = dataContext.Items.FirstOrDefault(x => x.ItemID == id);
    var cm = new CustomModel();
    cm.ItemList = dataContext.GetItem_List(row);
    cm.Description = row.Description;

    return View(cm);
 }
[HttpPost]
public ActionResult UpdateItems(CustomModel cm)
{

   return RedirectToAction("Index");
}
@model TestWeb.Models.CustomModel


@using (Html.BeginForm("UpdateItems", "Item", FormMethod.Post))
{

     <table>
            <tr>
                <th>Name</th>
                <th>Description</th>
            </tr>
        @foreach (var p in Model.ItemList.ToList())
        {
            <tr>
                <td>
                    @Html.HiddenFor(mdl => p.ItemId)
                </td>
                <td>@p.Name</td>
                <td>
                    @Html.EditorFor(mdl => p.Description)
                </td>
            </tr>
        }
    </table>

    <p>
        <input type="submit" value="save" />
    </p>
}
这是一种观点:

 public ActionResult Details(int id)
 {
    var row = dataContext.Items.FirstOrDefault(x => x.ItemID == id);
    var cm = new CustomModel();
    cm.ItemList = dataContext.GetItem_List(row);
    cm.Description = row.Description;

    return View(cm);
 }
[HttpPost]
public ActionResult UpdateItems(CustomModel cm)
{

   return RedirectToAction("Index");
}
@model TestWeb.Models.CustomModel


@using (Html.BeginForm("UpdateItems", "Item", FormMethod.Post))
{

     <table>
            <tr>
                <th>Name</th>
                <th>Description</th>
            </tr>
        @foreach (var p in Model.ItemList.ToList())
        {
            <tr>
                <td>
                    @Html.HiddenFor(mdl => p.ItemId)
                </td>
                <td>@p.Name</td>
                <td>
                    @Html.EditorFor(mdl => p.Description)
                </td>
            </tr>
        }
    </table>

    <p>
        <input type="submit" value="save" />
    </p>
}
@model TestWeb.Models.CustomModel
@使用(Html.BeginForm(“UpdateItems”、“Item”、FormMethod.Post))
{
名称
描述
@foreach(Model.ItemList.ToList()中的var p)
{
@Html.HiddenFor(mdl=>p.ItemId)
@p、 名字
@EditorFor(mdl=>p.Description)
}

}

我做错了什么?

你读过这篇博文吗?Steve介绍了asp mvc中的列表编辑

阅读完本nuget软件包后,请尝试以下操作:

制作一个
GetItems\u ListResult.cshtml
如下:

<tr>
 <td>
    @Html.HiddenFor(mdl => mdl.ItemId)
 </td>
  <td>@Model.Name</td>
 <td>
    @Html.EditorFor(mdl => mdl.Description)
 </td>
</tr>

“长度”未显示在intellisense列表中。相反,我必须这样做:Model.ItemList.ToList().Count。但使用它给我的索引时出现以下错误消息:无法将带[]的索引应用于“System.Data.Linq.ISingleResult”类型的表达式。此外,您还提到“在foreach循环中执行此操作”。我必须在foreach中添加for循环吗?它包含60多个项目。dbml根据存储过程生成ISingleResult。我不知道为什么,因为SP返回的项目不止1个。@Yustme您可以尝试在dbml设计器中更改CustomerModel,并创建类型
列表
,然后执行for循环。另外,它与常规foreach一起工作吗?它不是CustomerModel,而是CustomModel。它结合了两个模型,因此我可以为视图创建一个强类型模型。否则,我必须将2个模型传递给View(),这是不可能的。更改dbml会使更新后的所有操作都撤消。忘了提到问题是视图中没有显示数据。那很好用。所有数据都显示得很好。但在按下提交按钮后,将其发送到控制器是一个问题。参数“cm”始终为空。数据丢失了,我得到了一条记录,所有值都是“0”或“null”。我看到了那个博客。但他用的是aspx,我用的是razor引擎,原理完全一样。目前,我正在基于razor的生产系统中使用它。实际的MVC基础设施是相同的。