在同一视图上提交多个按钮

时间:2013-01-10 10:21:41

标签: asp.net-mvc-4 visual-studio-2012

我需要在同一视图上有多个按钮,但由于某种原因它不起作用。我已经看过同样的问题,但我还没有找到解决方案。

我如何知道视图中执行了哪个按钮?

查看:

<%@ Page Title="" Language="C#" MasterPageFile="~/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<LIASWeb.Models.Publication>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>NewPublication</h2>

    <% using (Html.BeginForm())
       { %>
    <div>
        <fieldset>
            <p>
                <label for="Name">Naam:</label>
                <%: Html.TextBoxFor(m => Model.nmPublication) %>
            </p>
            <p>
                 <%: Html.TextBox("Search", "type naam")%>
                <input type="submit" name="btnSearch" value="Zoeken" />
            </p>
            <p>
                <input type="submit" name="btnSave" value="Opslaan" />
            </p>
        </fieldset>
    </div>
    <% } %>
</asp:Content>

控制器:

public class PublicationController : Controller
{
    private Repository repository = null;

    public PublicationController()
    {
        repository = new Repository();
    }

    public ActionResult NewPublication(String button)
    {
        if (button == "btnSave")
            // perform save action

            if (button == "btnSearch")
            // perform save action

        return View();
    }

    public ActionResult Index()
    {
        IEnumerable<Publication> model = repository.Publications;
        return View(model);

}

工艺路线:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Publication", action = "Index", id = UrlParameter.Optional }
        );
}

5 个答案:

答案 0 :(得分:0)

您应该在一个表单中使用一个提交按钮。

不同形式的不同控制器方法是最好的方式恕我直言

答案 1 :(得分:0)

这是我之前为类似向导的视图所做的事情,这是可能的,但我不认为你的方法会起作用。

相反,我建议尝试使用基于this post

的解决方案

您也可以不使用表单元素进行提交,也可以使用jQuery / Javascript提交表单。

答案 2 :(得分:0)

这个问题有很多解决方案:

选项1 -

有类似的东西(我没有检查代码是否正确,所以请耐心等待):

@using (Html.BeginForm("Controller", "Action1", FormMethod.Post, new { id="MyForm1"}))
{
   <button type="submit" id="btn1">Bt1</button>
}

@using (Html.BeginForm("Controller", "Action2", FormMethod.Post, new { id="MyForm2"}))
{
   <button type="submit" id="btn2">Bt2</button>
}

现在你指的是两个不同的动作,你可以清楚地对它们进行编程,你仍然可以使用View("SameViewForBothForms")或重定向到“MainView”返回它们

选项2 - 只有一个带有2个按钮的表单&gt;不提交类型,而是简单的按钮,你将有一个Javascript函数来改变隐藏字段“buttonName”的值(在JS函数中你改变这个值)。

选项3 - 任何形式的多种或单一形式的混合都是可能的......

答案 3 :(得分:0)

试试这个解决方案:

public ActionResult NewPublication(string btnSearch)
{
    if (!string.IsNullOrEmpty(btnSearch))
    {
        //btnSearch was clicked
    }
    else
    {
        //btnSave was clicked
    }
}

同时检查此thread

希望它有所帮助。

答案 4 :(得分:0)

您可以从下面的名称标签中识别您的按钮,您需要在控制器中进行检查

if (Request.Form["btnSearch"] != null)
{
//Write your code here
}
else if (Request.Form["btnSave"] != null)
{
//Write your code here
}
相关问题