2013-01-10 32 views
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 } 
     ); 
} 
+0

的[你如何處理多個提交按鈕在ASP.NET MVC框架可能重複的識別您的按鈕?](http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – nemesv

+0

這個問題已經超過4天了,你確定目前的答案是不是「正確的答案」?如果是的話,那麼選擇它作爲正確的答案,如果沒有,那麼請給我們更多關於你的問題的細節。謝謝。 – Dryadwoods

回答

0

您應該使用一個以某種形式提交按鈕。

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

0

這件事我已經爲精靈般的美景之前完成,這是可能的,但我不認爲你的方法會奏效。

相反,我建議您嘗試,我用的解決方案,它是基於this post

你也只是不使用表單元素的提交,並提交使用jQuery/JavaScript的過於形式。

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- 任何一種的多個或單個形式的混合是可能的....

0

嘗試這種解決方案:

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

入住這thread以及

希望它有助於。

0

,你可以從那裏名字標籤象下面這樣,你需要檢查像這樣在你的控制器

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