在NerdDinner MVC应用程序演示中,有在设置ActionResult Delete时定义的confirmButton
public ActionResult Delete(int id, string confirmButton) {confirmButton是用来做什么的,因为它没有在代码中使用?我假设它返回了所单击的submit按钮的名称,但它只是一个空字符串。你怎么才能知道哪个按钮被按下了(例如,你可以在同一页上有archive和一个delete (或者yes,no)按钮)?
发布于 2010-08-09 22:31:08
如果你查看视图Delete.aspx,你会看到下面的html...
<h2>
Delete Confirmation
</h2>
<div>
<p>Please confirm you want to cancel the dinner titled:
<i> <%:Model.Title %>? </i> </p>
</div>
<% using (Html.BeginForm()) { %>
<input name="confirmButton" type="submit" value="Delete" />
<% } %>如您所见,confirmButton位于此处,该值将被传递给您指定的ActionResult。
您还可以指定两个按钮,如...
<% using (Html.BeginForm()) { %>
<input name="confirmButton" type="submit" value="Delete" />
<input name="confirmButton" type="submit" value="Something Else" />
<% } %>confirmButton参数将具有您单击的值...
虽然很奇怪为什么它在NerdDinner中不能正常工作,但您可以通过创建一个快速项目并打开默认的HomeController并添加
[HttpPost]
public ActionResult Index(string confirmButton) {
return View();
}在Index.aspx中,您可以添加
<% using (Html.BeginForm()) { %>
<input name="confirmButton" type="submit" value="Delete" />
<input name="confirmButton" type="submit" value="Something Else" />
<% } %>你应该可以走了。
https://stackoverflow.com/questions/3440886
复制相似问题