0

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

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

看法:

<%@ 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 }
        );
}
4

5 回答 5

0

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

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

于 2013-01-10T10:47:27.947 回答
0

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

相反,我建议尝试我使用的基于这篇文章的解决方案

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

于 2013-01-10T11:15:11.700 回答
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>
}

现在您指向 2 个不同的操作,您可以在其中清楚地对它们进行编程,并且您仍然可以使用View("SameViewForBothForms")或重定向到“MainView”来返回它们

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

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

于 2013-01-10T11:26:30.813 回答
0

试试这个解决方案:

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

也检查这个线程

希望能帮助到你。

于 2013-01-10T12:25:43.943 回答
0

您可以从那里的名称标签中识别您的按钮,如下所示,您需要在控制器中进行这样的检查

if (Request.Form["btnSearch"] != null)
{
//Write your code here
}
else if (Request.Form["btnSave"] != null)
{
//Write your code here
}
于 2016-05-04T11:25:43.143 回答