【问题标题】:How can I get a list of objects in a controller using Html.BeginForm? [duplicate]如何使用 Html.BeginForm 获取控制器中的对象列表? [复制]
【发布时间】:2016-05-12 19:13:24
【问题描述】:

我在向控制器发送对象列表时遇到问题。出于某种原因,我不断得到一个 count=0 的列表。

这是我发送的模型:

public class DIYViewModel
{

    public List<Item> Items { get; set; }
    public List<CheckModel> Checklist { get; set; }
    public int Page { get; set; }
    public int TotalPages { get; set; }
    public DIYViewModel(int page, List<Item> items,List<CheckModel> checklist)
    {
        int index = (page - 1) * 10;
        this.Items = items.Skip(index).Take(10).ToList();
        this.Page = page;
        this.TotalPages = ((items.Count - 1) / 10) + 1;
        this.Checklist = checklist;
    }
}

这是检查模型:

public class CheckModel
{
    public int Id
    {
        get;
        set;
    }
    public string Name
    {
        get;
        set;
    }
    public bool Checked
    {
        get;
        set;
    }
}

这是获得 DIYViewModel 的视图:

@model Homeserve.Web.Models.DIYViewModel
@using Sitecore.Data.Items
@using Homeserve.Web.Models.Helpers
@using (Html.BeginForm("Testing", "DIY", new {  app=Model.Checklist }))
{
foreach (CheckModel item in Model.Checklist)
{
    @Html.HiddenFor(it => item.Id)
    @Html.DisplayFor(it => item.Name)
    @Html.CheckBoxFor(it => item.Checked)
}
<input id="Submit1" type="submit" value="submit" />
}
@{
foreach (Item newsItem in Model.Items)
{
    <p> @Html.Sitecore().Field("Article Title", newsItem)</p>
    <p> @Html.Sitecore().Field("Article Date", newsItem)</p>
    <p> @Html.Sitecore().Field("Contents", newsItem)</p>
    <p> @Html.Sitecore().Field("Article Image", newsItem)</p>
    <p>social media stuff here</p>
}
}
@Html.DIYsPagination(Model.Page, Model.TotalPages)

我的控制器:

[HttpPost]
public ActionResult Testing(List<CheckModel> app)
{
    return View();
}

列表应用程序始终是一个包含 0 个项目的列表,即使在视图中一切正常。

【问题讨论】:

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


    【解决方案1】:

    您必须使用for 循环而不是foreach,并使用索引来保存数据。

    @for (int i = 0; i < Model.Checklist.Count ;i++)
    {
        @Html.DisplayFor(it => it.Checklist[i].Name)
        @Html.HiddenFor(it =>  it.Checklist[i].Id)
        @Html.CheckBoxFor(it =>  it.Checklist[i].Checked)
    }
    

    【讨论】:

    • 另外,你在视图中的模型是@model DIYViewModel,所以你的POST方法需要是public ActionResult Testing(DIYViewModel app)
    • @StephenMuecke 我也试过这样但是一直为空,我试试用 DIYViewModel 作为模型的 for 方法
    • @user1705561,是的,它必须是for 循环(或使用自定义EditorTemplateCheckModel 并在视图中使用@Html.EditorFor(m =&gt; m.Checklist)foreach 循环生成重复的@ 987654331@ 与您的模型无关的属性和重复的id 属性是无效的 html(您应该在执行此操作之前和之后检查实际的 html 以了解)
    • @StephenMuecke 我尝试使用 for 和我发送的模型,但现在不是传递 count=0,而是传递 null。我有另一个问题,代码现在看起来像这样:@using (Html.BeginForm("Testing", "DIY", new { app= Model })) { for (int i = 0; i it.Checklist[i].Name) @Html.HiddenFor(it => it.Checklist[i].Id) @Html.CheckBoxFor(it => it.Checklist[ i].Checked) } } 我发送的模型有比设置显示更多的字段,我需要
    • 也添加这些?这也是它生成的 html: Cooling 第一项。
    猜你喜欢
    • 2019-09-05
    • 2019-09-05
    • 1970-01-01
    • 2017-05-02
    • 2014-09-11
    • 1970-01-01
    • 2012-07-02
    • 2019-01-08
    • 1970-01-01
    相关资源
    最近更新 更多