Saturday, 6 February 2016

How to handle multiple button in single view and same form on submit in ASP.NET MVC?

There might be situation where you have to use multiple button in same form. Then what would you do ?

  If you don't want to mess up more in this, then let me scroll you to our main topic and show you how to do it.





































Now you can see in code above I have given  all 4 buttons same name as command.

 <button type="submit" class="btn btn-default" name="command" id="btnMonth" value="Month" style="margin-top:25px;">Submit</button>

<button type="submit" class="btn btn-default" name="command" id="btnDay" value="Day" style="margin-top:25px;">Submit</button>

And on form submit call action as shown:

@using (Html.BeginForm("MultipleCommand", "Default", FormMethod.Post, new { id = "Form1" }))
{
}

So on button click MultipleCommand of Default controller would be called and we can use it in the as shown below.



That's it Over.

Use if else condition as if command =="blabla" {// Your code to execute } and finally return as you want.

Thanks.

No comments:

Post a Comment

How To Pass An Array As A Parameter While Calling an ASP.NET Web API C#

Please visit my C# Corner Blog for this, where I have provided better efforts to make this concept more understandable - www.c-sharpcorner....