开发者

Bind in controller with multiple equals prefix

i have a single form in my project (MVC3 C# with razor) and this is a part of the code (there are several buttons of type submit):

@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>                 
               }
            }

And in my controller, i have a action method that process the form like this:

[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,
                                        开发者_Go百科      [Bind(Prefix = "tag")] Keyword tag,
                                              [Bind(Prefix = "note")] Note note,
                                              [Bind(Prefix = "web")] WebProfile web,
                                              [Bind(Prefix = "connection")] Connection connection){

As you can see, the ProcessEditForm receives a FormCollection and with this, i can know which button has pressed in the same form on the view side, but my problem is the foreach iteration in the form, because when i whant to get the Communication that corresponds with the button, i get the first one because, the Prefix is the same in the iteration. How can i solve this?


I think you are looking at your architecture the wrong way - in my opinion you should only have one submit button on a form, after all a form is for submitting values.

I would restructure your controller / views, and instead of using multiple submit buttons I would use action links that link to other actions in the controller, e.g. edit, save etc.

As you are iterating the mail objects you can create the action link and use the Hash property as an action parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜