0

目的:

私はAjax.BeginFormの投稿を持っており、私の目的はコントローラーでボタンIDを取得することです。Html.BeginFormを使用した例を見てきましたが、Ajaxフォームが必要です。

コード: C#MVC3

意見:

@using (Ajax.BeginForm("Save", "Valoration", new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "HvmDetailTabStrip", OnSuccess = "OnSuccessSaveValoration" }))
{ 
    <div id ="HvmDetailTabStrip">
        @(Html.Partial("_ValorationDetail"))
    </div>
    <button type="submit" style="display:none" id="db1"></button>       
    <button type="submit" style="display:none" id="db2"></button>       
}        

コントローラ:

[HttpPost]
public ActionResult Save(ValorationModel model)
{
    if ("db1")    
    {
        var result = ValorationService.Save(ValorationModel);
    }
    else
    {
        // ....
    }         

    return PartialView("_ValorationDetail", ValorationModel);
}
4

1 に答える 1

2

ボタンの値は次のように取得できます。

@using (Ajax.BeginForm("Save", "Valoration", new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "HvmDetailTabStrip", OnSuccess = "OnSuccessSaveValoration" }))
    { 
        <div id ="HvmDetailTabStrip">
                @(Html.Partial("_ValorationDetail"))
        </div>
        <button type="submit" name="submitButton" value="db1"></button>       
        <button type="submit" name="submitButton" value="db2"></button>       
    }

そして、コントローラーで次のように記述できます。

[HttpPost]
    public ActionResult Save(ValorationModel model)
    {
       string buttonValue = Request["submitButton"];

       if(buttonValue == "db1"){
        var result = ValorationService.Save(ValorationModel);
       }else
       {
          ....
       }         

        return PartialView("_ValorationDetail", ValorationModel);
    }

または、メソッドに渡すパラメーターの数が重要でない場合は、これを使用できます。

[HttpPost]
        public ActionResult Save(ValorationModel model, string submitButton)
        {
           if(submitButton == "db1"){
            var result = ValorationService.Save(ValorationModel);
           }else
           {
              ....
           }         

            return PartialView("_ValorationDetail", ValorationModel);
        }

あなたの問題を解決する他の方法はここにありますASP.Net MVC - 同じ値のボタンを送信

于 2012-12-11T09:05:23.147 に答える