1

So I have this form:

@using (Html.BeginForm("SendMailAsACompany", "Contract", FormMethod.Post, new { id = "cvr" }))
       {
        <input type="hidden" value=@Model name="studentId" />
        <input type="hidden" value=@Model.Project.UserId name="companyId" />
        <input type="hidden" value=@Model.ApplicationId name="applicationId"/>
        <input type="text" name="companyCVR" placeholder="Indsæt CVR-nr." required/>
       }

Then at the bottom of the page I have the submit button:

<button form="cvr" class="btn btn-success">Submit</button>

I want to add a second button that would submit the same data but call another method instead of "SendMailAsACompany". Is there a way to modify the button/form to accommodate this change?

crystyxn
  • 1,411
  • 4
  • 27
  • 59

1 Answers1

1

Yes, you can change the html form attribute dynamically with jqyery/javascript

this is your 1st button:

<button form="cvr" class="btn btn-success">Submit</button>

this is the other one which will submit the data on another action method:

<input type="button" id="btnSave" onclick="SubmitForm()" value="save"/>

function SubmitForm()
{
 $("#cvr").attr("action", "your controller/your new action method");
$('#cvr').submit();
}

I hope it would be helpful! If something is not clear here plz let me know.

R K Sharma
  • 845
  • 8
  • 23
  • 42
  • If so I would need to modify it to "$("#cvr").attr("SendMailAsACompany", "SecondMethod");", correct? – crystyxn Jul 28 '16 at 11:57
  • yes, its html form, after submission of form the page will get reload and the form action will be reset to "SendMailAsACompany" :) – R K Sharma Jul 28 '16 at 12:02
  • 1
    I got it to work by using " $("#cvr").attr("action", "Contract/List");" !! :) – crystyxn Jul 28 '16 at 12:09
  • 1
    yo man! that's it. you have to specify the full url. If this code is on your .cshtml page then your can also use $("#cvr").attr("action", '@url.Action("your action method","your controller name")'); – R K Sharma Jul 28 '16 at 12:18
  • 1
    you too bro. happy to help :) – R K Sharma Jul 28 '16 at 12:35