【问题标题】:Is it possible to Pass set of table records from front end to back end using MVC3是否可以使用 MVC3 将一组表记录从前端传递到后端
【发布时间】:2014-06-01 11:13:31
【问题描述】:

我在前端显示一个表格,其中表格记录是可编辑的,每条记录都有一个复选框。

如果通过勾选相应的复选框并提交选择了某些记录,则所有选择的记录都必须存储在后端表中。

是否可以使用 Asp.net MVC3?

如果是,如何将该记录列表从视图传递到控制器和模型?

【问题讨论】:

  • 您可以使用 JSON 和 Ajax 将值从 View 传递到 Controller。请发布您尝试过的内容(示例代码)。如果您不想使用 Ajax,可以查看 Dan Ludvig 的 BeginCollectionItem 助手 (nuget.org/packages/BeginCollectionItem/1.2.1)。请在帖子中提供有关您的查询的更多信息。

标签: asp.net ajax asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

据我了解,您希望在发布期间将选定复选框的数组传递给 ActionResult。 请参阅下面的示例

型号:

public class EditableItemModel
{
    public bool SelectedItem { get; set; }
    public string Name { get; set; }
}

控制器:

    public ActionResult Index()
    {
        var model = new EditableItemModel[]
                        {
                            new EditableItemModel { SelectedItem = true },
                            new EditableItemModel()
                        };

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(EditableItemModel[] tableItems)
    {
        IEnumerable<EditableItemModel> selectedItems = tableItems.Where(i => i.SelectedItem);

        return RedirectToAction("Index");
    }

查看:

@using CheckboxInTableExample.Models;
@model EditableItemModel[]

@using (Html.BeginForm("Index"))
{   
    <table>
    @for (int i = 0; i < Model.Length; i++)
    {
         <tr>
             <td>
                    @Html.CheckBoxFor(m => m[i].SelectedItem)
             </td>  
             <td>
                    @Html.EditorFor(m => m[i].Name)
             </td>      
         </tr>
    }
    </table>
    <input type="submit" title="Submit" />
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多