2013-08-02 106 views
3

我有两个看法,一个是CustomerDetail.cshtml,另一个是PAymentDetail.cshtml,我有一个控制器QuoteController.cs。我可以在asp.net mvc控制器中使用多个Post方法吗?

对于这两个视图都有提交按钮和HTTPPOST方法都在QuoteController.cs中。

[HttpPost] 
public ActionResult CustomerDetail(FormCollection form) 
{ 
} 

[HttpPost] 
public ActionResult PAymentDetail(FormCollection form) 
{ 
} 

现在,当我点击提交的支付信息按钮,它呼吁/路由HttpPost为CustomerDetail而非PAymentDetail的方法。

任何人都可以帮助我吗?我做错了什么?表单方法都是POST。

+0

您可能想要检查您的路由配置,特别是如果您已将其从默认设置修改。 – MushinNoShin

回答

4

对于PaymentDetail,您可以在视图使用:

@using(Html.BeginForm("PAymentDetail","Quote",FormMethod.Post)) 
{ 
    //Form element here 
} 

结果HTML将

<form action="/Quote/PAymentDetail" method="post"></form> 

同为顾客详细

@using(Html.BeginForm("CustomerDetail","Quote",FormMethod.Post)) 
{ 
    //Form element here 
} 

希望有所帮助。只要这些方法具有不同的名称,在同一个控制器中使用两种post方法并不是问题。

对于除FormCollection以外的更好的方法,我推荐这个。 首先,创建一个模型。

public class LoginModel 
{ 
    public string UserName { get; set; } 
    public string Password { get; set; } 
    public bool RememberMe { get; set; } 
    public string ReturnUrl { get; set; } 

} 

然后,在视图中:

@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> 

}

这些用户输入将被映射到控制器中。

[HttpPost] 
public ActionResult Login(LoginModel model) 
{ 
    String username = model.Username; 
    //Other thing 
} 

祝你好运。

1

绝对!只要确保您发布的是正确的操作方法,请检查您呈现的HTML的form标签。另外,FormCollection不是一个好的MVC设计。

+0

你可以在一些好的设计上添加一些信息:)喜欢参加'class'或类似的。 – NoLifeKing

+0

我有一些标签,不是使用剃须刀视图创建的,这些值需要在控制器方法中捕获。你是否知道这样做的更好方式(FormCollection除外)。而且,我们可以在同一个控制器中使用两种后置方法吗? – PaRsH

相关问题