【问题标题】:Can I modify multiple items in a BeginForm section in a Razor/MVC view [duplicate]我可以在 Razor/MVC 视图的 BeginForm 部分修改多个项目吗?
【发布时间】:2015-08-26 21:41:55
【问题描述】:

我想编辑一个项目列表并将它们全部保存在同一个提交中。是否可以?如果有,怎么做?

我有以下代码,但它没有给出想要的结果。否则我不知道为控制器中的对应写什么。

@using (Html.BeginForm("Save", "MyController", FormMethod.Post))
{
<fieldset>
    <table class="table table-striped table-hover ">
        <thead>
            <tr>
                <th>Name</th>
                <th>Value</th>
                <th>Datum</th>
                <th>NewValue</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.HiddenFor(modelItem => item.Id)
                        @Html.DisplayFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Value)
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => item.Value)
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

更新感谢@Jonesopolis,我想出了这个可行的解决方案

@using (Html.BeginForm("Save", "MyController", FormMethod.Post))
{
<fieldset>
    <table class="table table-striped table-hover ">
        <thead>
            <tr>
                <th>Name</th>
                <th>Value</th>
                <th>Datum</th>
                <th>NewValue</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>
                        @Html.HiddenFor(modelItem => modelItem[i].Id)
                        @Html.DisplayFor(modelItem => modelItem[i].Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => modelItem[i].Value)
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => modelItem[i].Value)
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

对于我的控制器

public class MyController : Controller
{
    [HttpPost]
    public ActionResult Save(IEnumerable<NameDoesNotMatter> newValues)
    {
        ...
    }
}

public class NameDoesNotMatter
{
    public int Id { get; set; }
    public decimal? Value { get; set; }
}

现在看看我是否可以使用模板解决问题。 @StephenMuecke的链接应该足够了

更新 2 好吧,现在的代码并不难

@using (Html.BeginForm("Save", "MyController", FormMethod.Post))
{
<fieldset>
    <table class="table table-striped table-hover ">
        <thead>
            <tr>
                <th>Name</th>
                <th>Value</th>
                <th>Datum</th>
                <th>NewValue</th>
            </tr>
        </thead>
        <tbody>
            @Html.EditorForModel()
        </tbody>
    </table>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

对于 Views/Shared/EditorTemplates/TypeOfModel.cshtml

@model TypeOfModel
<tr>
    <td>
        @Html.HiddenFor(item => item.Id)
        @Html.DisplayFor(item => item.Name)
    </td>
    <td>
        @Html.DisplayFor(item => item.Value)
    </td>
    <td>
        @Html.TextBoxFor(item => item.Value)
    </td>
</tr>

控制器保持不变

【问题讨论】:

  • 是的,这很有可能。在这里使用for 循环而不是foreach 循环。您的提交应将集合返回给控制器。
  • 请参阅 this answer 了解为什么需要 for 循环或 EditorTemplate
  • 我想我可以使用这些答案......会尝试。
  • 我想接受你的一个答案,但无法接受评论
  • 没问题(反正我已经把它标记为重复)——当你获得必要的代表时,你总是可以投票给其他答案

标签: c# asp.net-mvc asp.net-mvc-4 razor html.beginform


【解决方案1】:

当您仅使用 for 循环显示项目时,生成的 html 标记中使用的命名约定与 MVC 在表单提交时绑定模型时所期望的不匹配。解决此问题的最简单方法是使用 @Html.EditorFor 和自定义编辑器模板。以下是几个例子:

ASP.NET MVC DisplayTemplate and EditorTemplates for Entity Framework DbGeography Spatial Types

Extending Editor Templates for ASP.NET MVC

【讨论】:

  • 我找不到一个有用的例子,如何结合使用模板和 for 循环
猜你喜欢
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多