我在列出多个表单时遇到问题,包括提交按钮。 当点击第一个按钮,它正确的帖子,但是当我点击第二个提交按钮将其提交一个空集... 下面是我的代码:mvc4页面上的多个表单,提交一个表单
Index.cshtml
@model MVCFormsSubmitting.Models.FormsRepository
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@for (int i = 0; i < Model.UserInfo.Count; i++)
{
using (Html.BeginForm("Index", "Forms", FormMethod.Post, new {name = "Form" + @i}))
{
<b>@Html.EditorFor(m => m.UserInfo[i].Name)</b>
<input name="button @i" type="submit" class="left btn btn-primary" value="Ret navne">
<br/>
}
}
Formsrepository.cs
namespace MVCFormsSubmitting.Models
{
public class Info
{
public int Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
public class FormsRepository
{
public FormsRepository()
{
this.UserInfo = new List<Info>();
}
public List<Info> UserInfo { get; private set; }
public async Task Load()
{
this.UserInfo.Clear();
UserInfo = await LoadUsers();
}
public async static Task<List<Info>> LoadUsers()
{
List<Info> info = new List<Info>();
info.Add(new Info(){
Age = "32,",
Email = "[email protected]",
Name = "John Doe",
Phone = "123456749",
Id = 0
});
info.Add(new Info()
{
Age = "36",
Email = "[email protected]",
Name = "Jane Doe",
Phone = "987654321",
Id = 1
});
return info;
}
}
}
FormsController.cs
public class FormsController : Controller
{
//
// GET: /Forms/
public ActionResult Index()
{
FormsRepository.Load();
return View(FormsRepository);
}
[HttpPost]
public ActionResult Index(FormsRepository text)
{
return RedirectToAction("Index");
}
private static FormsRepository _repository;
public static FormsRepository FormsRepository
{
get
{
if (_repository == null)
{
_repository = new FormsRepository();
}
return _repository;
}
}
}
当在Formscontroller的HttpPost动作设置断点,你会看到,当点击提交的第一个按钮就会发出1项,但点击第二个按钮的产品空当......
请帮助:)
你有没有注意到,每提交和表格具有相同的ID?还有一件事'按钮@i'在词语之间留有空白!看一看,让我知道! – Fals