Why does BeginRouteForm write a class name to html?
I'm experimenting with ASP.NET MVC 2, and I have a simple form using Html.BeginRouteForm
, which is working file, except it writes the string System.Web.Mvc.Html.MvcForm
to the html.
Why does it do this and how can I get it to stop?
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inh开发者_如何学Cerits="System.Web.Mvc.ViewPage<SelectList>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<%=Html.BeginRouteForm(String.Empty, new {Action = "Search"}, FormMethod.Get) %>
<%:Html.DropDownList("worklist", this.Model )%>
<br />
<input type="submit" />
<% Html.EndForm(); %>
</asp:Content>
<h2>Index</h2>
<form action="/Site/Search" method="get">System.Web.Mvc.Html.MvcForm
<select Label="Worklists" id="worklist" name="worklist"><!-- Options -->
</select>
<br />
<input type="submit" />
</form>
Because you're calling it incorrectly. <%=
means, more or less, "treat the result as a string." The default implementation of .ToString()
writes the class name. You could use <%
and that would fix the problem. But better still is to use it with using
, which is more idiomatic for MVC and relieves you of the need to call EndForm
explicitly. In other words, change your code:
<%=Html.BeginRouteForm(String.Empty, new {Action = "Search"}, FormMethod.Get) %>
...
<% Html.EndForm(); %>
... to:
<% using (Html.BeginRouteForm(String.Empty, new {Action = "Search"}, FormMethod.Get)) { %>
...
<% } %>
精彩评论