我需要在同一視圖上有多個按鈕,但由於某些原因,它不起作用。我已經看過相同的問題,但我還沒有找到解決方案。在同一視圖中提交多個按鈕
如何知道在視圖中執行哪個按鈕?
查看:
<%@ 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 }
);
}
的[你如何處理多個提交按鈕在ASP.NET MVC框架可能重複的識別您的按鈕?](http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – nemesv
這個問題已經超過4天了,你確定目前的答案是不是「正確的答案」?如果是的話,那麼選擇它作爲正確的答案,如果沒有,那麼請給我們更多關於你的問題的細節。謝謝。 – Dryadwoods