3

1 つは CustomerDetail.cshtml で、もう 1 つは PAymentDetail.cshtml で、1 つのコントローラー QuoteController.cs があります。

両方のビューには [送信] ボタンがあり、両方のビューの HTTPPOST メソッドは QuoteController.cs にあります。

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

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

今、支払いの詳細の [送信] ボタンをクリックすると、PAymentDetail ではなく CustomerDetail の HttpPost メソッドが呼び出されます。

誰かがこれについて私を助けることができますか?? 私が間違っているのは何ですか?両方 フォーム メソッドは POST です。

4

3 に答える 3

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
}

その助けを願っています。これらのメソッドの名前が異なる限り、同じコントローラーに 2 つの 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
}

頑張ってください。

于 2013-08-02T15:05:20.670 に答える
0

URL を 1 つだけにしたい場合は、別の方法があります: http://www.dotnetcurry.com/ShowArticle.aspx?ID=724

アイデアは、フォーム要素 (ボタンまたは隠し要素) を使用して、どのフォームが送信されたかを判断することです。次に、呼び出されるアクションを決定するカスタム アクション セレクター ( http://msdn.microsoft.com/en-us/library/system.web.mvc.actionmethodselectorattribute.aspx ) を記述します。

于 2013-08-03T09:49:48.293 に答える