【发布时间】:2011-05-20 16:12:44
【问题描述】:
我的项目中有一个表单(带有剃刀的 MVC3 C#),这是代码的一部分(有几个提交类型的按钮):
@foreach(Communication mail in Model.connectionToEdit.Comunications.Where(co => !co.Deleted && co.IdComunicationType == 4)) {
if(mail.IdComunication != 0) {
<input type="hidden" name="mailEdit.Hash" value="@mail.Hash" />
<input type="hidden" name="mailEdit.IdComunication" value = "@mail.IdComunication" />
<input type="hidden" name="mailEdit.IdComunicationType" value="4" />
<input type="email" name="mailEdit.Value" value="@mail.Value" />
<button type="submit" name="editmail" value="@mail.Hash" >edit</button>
}
else {
@Html.Partial("_communication", mail)<button type="submit" name="removemail" value="@mail.Hash">remove</button>
}
}
在我的控制器中,我有一个像这样处理表单的操作方法:
[HttpPost]
public ActionResult ProcessEditForm(FormCollection form,
[Bind(Prefix = "mail")] Communication mail,
[Bind(Prefix = "mailEdit")] Communication mailEdit,
[Bind(Prefix = "phone")] Communication phone,
[Bind(Prefix = "location")] Location location,
[Bind(Prefix = "keyword")] Keyword keyword,
[Bind(Prefix = "tag")] Keyword tag,
[Bind(Prefix = "note")] Note note,
[Bind(Prefix = "web")] WebProfile web,
[Bind(Prefix = "connection")] Connection connection){
如您所见,ProcessEditForm 收到一个 FormCollection 并且有了这个,我可以知道 哪个按钮在视图侧以相同的形式按下,但我的问题是表单中的 foreach 迭代,因为当我想获得与按钮对应的通信时,我得到第一个,因为前缀是相同的在迭代中。 我该如何解决这个问题?
【问题讨论】:
标签: c# asp.net asp.net-mvc-3 c#-4.0 razor