我觉得我在这方面可能过于“WebForms”,但我还是要问一下。我在一个ASP.NET MVC项目中有一个表单,其中有一些输入字段和两个按钮。一个按钮用于“筛选”列表框中的内容。另一个按钮用于提交表单。我的视图大致如下:
<%using (Html.BeginForm())
{%>
<%=Html.TextBox("SearchItems") %>
<input type="submit" name="Command" value="Find" />
<%=Html.ListBox("SelectedItems", new MultiSelectList(model.AvailableItems,"Id", "Name", model.SelectedItems))%>
//Some other form fields.
<input type="submit" name="Command" value="Send" />
<%} %>
我的操作大致如下:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index([Bind(Prefix = "")]SendMessageRequest model)
{
if (model.Command == "Find")
return SearchItems(model);
if (model.Command == "Send")
return Send(model);
throw new Exception("Invalid command.");
}
这个方案是可行的——控制器根据被点击的按钮选择正确的操作。但这并不理想,因为输入按钮的值也是显示文本。如果我想翻译这个应用程序,那么操作将会出问题。为了解决这个问题,我可以使用