【问题标题】:Bind in controller with multiple equals prefix在控制器中绑定多个等号前缀
【发布时间】: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


    【解决方案1】:

    我认为您以错误的方式看待您的架构 - 在我看来,您应该在表单上只有一个提交按钮,毕竟表单是用于提交值的。

    我会重组你的控制器/视图,而不是使用多个提交按钮,我会使用链接到控制器中其他操作的操作链接,例如编辑、保存等

    在迭代邮件对象时,您可以创建操作链接并将 Hash 属性用作操作参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      相关资源
      最近更新 更多