asp.net mvc - can we identify the id of a button in mvc on httppost -
i have view has 2 submit buttons(save&close,save&new).
<span> <input type="button" value="save&new" name="save&new" id="savenew" /> </span> <span> <input type="button" value="save&close" name="save&close" id="saveclose" /> </span> <span>
when hit 1 of these buttons,the model data goes controller , hits post action
[httppost] public actionresult company(myproject.models.company company) { return view(); }
now company object has complete model data(such company.phonenumber,company.state etc etc). want identify id of button(either save&new or save&close) user clicked. both buttons click leads same actionresult(company) , want identify button click request came. cannot use @html.actionlink instead of input type =submit. need know id out using jquery.
give buttons same name:
<button type="submit" name="btn" value="save_new" id="savenew">save&new</button> <button type="submit" name="btn" value="save_close" id="saveclose">save&close</button>
and controller action take btn
string parameter.
[httppost] public actionresult company(myproject.models.company company, string btn) { if (btn == "save_new") { // form submitted using save&new button } else if (btn == "save_close") { // form submitted using save&close button } else { // form submitted using javascript or user // pressed enter key while being inside of input fields } return view(); }
also notice have used submit
buttons (type="submit"
) whereas in example have used simple buttons (type="button"
) not allow submitting html form.
Comments
Post a Comment