html - Multiple different submit buttons in one form -
i've tried implementing option explained in this article.
public class httpparamactionattribute : actionnameselectorattribute { public override bool isvalidname(controllercontext controllercontext, string actionname, methodinfo methodinfo) { if (actionname.equals(methodinfo.name, stringcomparison.invariantcultureignorecase)) return true; if (!actionname.equals("action", stringcomparison.invariantcultureignorecase)) return false; var request = controllercontext.requestcontext.httpcontext.request; return request[methodinfo.name] != null; } }
my controller actions:
[httpparamaction] [httppost] public virtual actionresult editaccouncement(_accouncementpostviewmodel m) [httpparamaction] [httppost] public virtual partialviewresult deleteannouncement(int id)
my form:
@using (ajax.beginform("action", ajaxoptions: new ajaxoptions() { httpmethod = "post", insertionmode = insertionmode.replace, updatetargetid = "announcement" + @model.id })) { //form values omitted <button type="submit" class="submitbutton" name="edit">change details</button> <button type="submit" class="submitbutton" name="delete">delete</button> }
however controller action being called still action method (which doesn't exist). missing something?
your problem caused mismatch between action names , button name attributes.
the value of name attribute on buttons needs match action names, so:
<button type="submit" name="editaccouncement">change details</button> <button type="submit" name="deleteannouncement">delete</button>
update: suggest different approach together. solution seems counter-intuitive me, , not particularly easy follow.
you use javascript (e.g. jquery) handle form submits 'manually', hooking 2 different event handlers buttons. 1 event make post editaccouncement (typo!) action , 1 make post deleteannouncement action.
i made mockup on jsfiddle demonstrates code: http://jsfiddle.net/wmwnj/3/
update 2: fixed typos in jsfiddle
Comments
Post a Comment