Reputation: 1320
I have two views, one is CustomerDetail.cshtml and another is PAymentDetail.cshtml and i have one controller QuoteController.cs.
Both the Views has Submit buttons and HTTPPOST method for both the views are in QuoteController.cs.
[HttpPost]
public ActionResult CustomerDetail(FormCollection form)
{
}
[HttpPost]
public ActionResult PAymentDetail(FormCollection form)
{
}
Now, when i click on Submit button of Payment details, it is calling/routing to HttpPost method of CustomerDetail rather than PAymentDetail.
Could anyone help me on this?? What i'm doing wrong? Both The form method is POST.
Upvotes: 3
Views: 5353
Reputation: 145
For the PaymentDetail, you use this in the view:
@using(Html.BeginForm("PAymentDetail","Quote",FormMethod.Post))
{
//Form element here
}
The result html will be
<form action="/Quote/PAymentDetail" method="post"></form>
The same for customer Detail
@using(Html.BeginForm("CustomerDetail","Quote",FormMethod.Post))
{
//Form element here
}
Hope that help. Having two post methods in the same controller is not a problems, as long as these methods have different names.
For a better way other than FormCollection, I recommend this. First, you create a model.
public class LoginModel
{
public string UserName { get; set; }
public string Password { get; set; }
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
}
Then, in the view:
@model LoginModel
@using (Html.BeginForm()) {
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
//Insted of razor tag, you can create your own input, it must have the same name as the model property like below.
<input type="text" name="Username" id="Username"/>
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
</div>
</fieldset>
}
These user input will be mapped into the controller.
[HttpPost]
public ActionResult Login(LoginModel model)
{
String username = model.Username;
//Other thing
}
Good luck with that.
Upvotes: 4
Reputation: 2459
If you want to have only one url, here is another approach: http://www.dotnetcurry.com/ShowArticle.aspx?ID=724
The idea is to use a form element (the button or a hidden element) to decide, which form has been submitted. Then you write a custom action selector (http://msdn.microsoft.com/en-us/library/system.web.mvc.actionmethodselectorattribute.aspx) which decides which action will be invoked.
Upvotes: 0
Reputation: 190925
Absolutely! Just ensure you are posting to the right action method, check your rendered HTML's form
tags.
Also, the FormCollection
isn't a good design for MVC.
Upvotes: 1