如何从FormCollection返回对象列表?

问题描述 投票:1回答:1

我正在使用带有for循环的表单,具体取决于将创建表单输入字段的数量。我想返回一个Formcollection列表。我知道formcollection只返回一个表单,但我想返回许多表单(列表)所以我的控制器方法只适用于1个表单我的控制器:

  [HttpPost]
    public ActionResult SecondStepProcessValid(FormCollection form)
    {
        List<Customer> itemList = new List<Customer>();
        int i = 0;

          itemList.Add(new Customer()
            {

                Name = form["Name"],
                Emailaddress = form["Email"],
                HouseNumber = Int32.Parse(form["HouseNumber"]),
                Zipcode = form["Zipcode"]

          });
        foreach (var c in itemList)
        {
            customerRepository.Create(c);
        }
        return View("ThirdStepProcess");

}    

我的看法

<form method="post" action="@Url.Action("SecondStepProcessValid")">
        @for (int i = 0; i < Model.Amount; i++)
        {

            <div class="space">
                <div><br /></div>
                <div>Guest@(@i + 1)</div>
            </div>

            <div class="space">
                <div>Name</div>
                <div>@Html.TextBox("Name", Model.NewCustomers[i].Name)</div>
            </div>

            <div class="space">
                <div>Email</div>
                <div>@Html.TextBox("Email", Model.NewCustomers[i].Emailaddress)</div>
            </div>

            <div class="space">
                <div>House Number</div>
                <div>@Html.TextBox("HouseNumber", Model.NewCustomers[i].HouseNumber)</div>
            </div>

            <div class="space">
                <div>Zipcode</div>
                <div>@Html.TextBox("Zipcode", Model.NewCustomers[i].Zipcode)</div>
            </div>
            <input type="hidden" name="Index" value="@i" />

        }
        <input type="submit" value="Step 3" />
    </form>
c# list formcollection html.beginform
1个回答
1
投票

您不能将多个表单一起发布。但您也可以在表单或多个List<T>中发布多个项目。

为此,您需要强烈键入视图,然后在操作中,您可以使用List<Customer>类型的参数或具有List<Customer>类型属性的容器视图模型,如下所示:

public class CustomersModel 
{
    public int Amount { get; set; }

    public List<Customer> NewCustomers{ get; set; }

    CustomersModel()
    {
        Amount = new List<Customer>();
    }
}

将您的视图更改为使用该模型强类型,并使用强类型帮助器渲染输入控件,以避免使用魔术字符串发生的愚蠢错字错误,并从模型和visual studio intellisence中受益,并从错误中编译时间安全性:

    @model YourNamespace.Models.CustomersModel

    @for (int i = 0; i < Model.Amount; i++)
    {

        <div class="space">
            <div><br /></div>
            <div>Gast @(@i + 1)</div>
        </div>

        <div class="space">
            <div>Name</div>
            <div>@Html.TextBoxFor(x => Model.NewCustomers[i].Name)</div>
        </div>

        <div class="space">
            <div>Email</div>
            <div>@Html.TextBoxFor(x =>  Model.NewCustomers[i].Emailaddress)</div>
        </div>

        <div class="space">
            <div>House Number</div>
            <div>@Html.TextBoxFor(x => Model.NewCustomers[i].HouseNumber)</div>
        </div>

        <div class="space">
            <div>Zipcode</div>
            <div>@Html.TextBox(For(x => Model.NewCustomers[i].Zipcode)</div>
        </div>
            @Html.HiddenFor(x=> Model.NewCustomers[i].Id,i)

    }

现在在控制器中,您可以拥有一个您强烈键入的参数:

[HttpPost]
public ActionResult SecondStepProcessValid(CustomersModel model)
{
    foreach (var c in model.NewCustomers)
    {
        customerRepository.Create(c);
    }
    return View("ThirdStepProcess");
}

希望它能给出主意。

© www.soinside.com 2019 - 2024. All rights reserved.