我怎样才能 return 来自 FormCollection 的对象列表?
How can I return a list of objects from a FormCollection?
我正在使用带有 for 循环的表单,具体取决于将创建的表单输入字段的数量。我想 return 一个 Formcollection 列表。我知道 formcollection 只有 return 一种形式,但我想 return 多种形式(列表)
所以我的控制器方法只适用于一种形式
我的控制器:
[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>
您不能将多个表单 post 合并在一起。但是您可以 post 表单中的多个项目或多个 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 智能和编译错误时间安全:
@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");
}
希望能给个思路。
我正在使用带有 for 循环的表单,具体取决于将创建的表单输入字段的数量。我想 return 一个 Formcollection 列表。我知道 formcollection 只有 return 一种形式,但我想 return 多种形式(列表) 所以我的控制器方法只适用于一种形式 我的控制器:
[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>
您不能将多个表单 post 合并在一起。但是您可以 post 表单中的多个项目或多个 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 智能和编译错误时间安全:
@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");
}
希望能给个思路。