【问题标题】:MVC3 razor how to handle button clicks for each of the dynamically created button setsMVC3 razor 如何处理每个动态创建的按钮集的按钮点击
【发布时间】:2012-04-29 01:44:48
【问题描述】:

在我的 MVC3 剃须刀视图上,我有一个“GetRecords”按钮。单击此按钮后,它将回发并检索多组记录。根据记录集的数量,它将动态重新生成视图并显示每组记录记录。它还将针对每组记录生成“保存”和“删除”按钮,以便用户查看每个记录集并可以从这些记录集中“保存”或“删除”数据。

这里我的问题是如何处理按钮点击。任何建议

[Httppost]
 public ActionResult GetRecords(ContentsViewModel vmodel)
        {
         vmodel.GetRecords();

       return view(vmodel);
     }

【问题讨论】:

    标签: asp.net-mvc-3 razor


    【解决方案1】:

    我不太确定您要实现的目标是什么,但如果您想处理表单中的多个按钮,那么在视图中您需要为每个按钮命名。例如:

    @using (Html.BeginForm())
    {
        ...
        <input type="submit" value="Get Records" name="getrecords"/>
        <input type="submit" value="Save" name="save"/>
        <input type="submit" value="Delete" name="delete"/>
    }
    

    然后您可以使用以下命令在 post 操作中测试这些值:

    [HttpPost]
    public ActionResult AppropriateActionNameHere(ContentsViewModel vmodel)
    {
        if (!string.IsNullOrEmpty(Request["getrecords"]))
        {
            vmodel.GetRecords();
        }
        else if(!string.IsNullOrEmpty(Request["save"]))
        {
            //save record processing here
        }
        else if(!string.IsNullOrEmpty(Request["delete"]))
        {
            //delete record processing here
        }
        return View(vmodel); //Or perform the appropriate redirect or whatever you need to perform
    }
    

    另一种可能性是在视图上有多个表单,每个表单回发到不同的操作。

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多