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 回答