MVC3中的Html.BeginForm渲染太多了

时间:2011-05-31 12:02:18

标签: asp.net-mvc asp.net-mvc-3

我有以下代码。请注意,我没有使用使用,因为我想开始并在我的代码的两个不同区域结束表单。有没有人看过帮助者添加“System.Web.Mvc.Html.MvcForm”的这个动作。我不明白为什么会这样添加。

Code:
 @Html.BeginForm(new { action = ViewContext.Controller.ValueProvider.GetValue("action").RawValue })

@{ Html.EndForm() }

Result:
<form action="/adminTests/Edit" method="post">System.Web.Mvc.Html.MvcForm

当我使用以下内容时,我没有得到“System.Web.Mvc.Html.MvcForm”

    @using (Html.BeginForm(new { action = ViewContext.Controller.ValueProvider.GetValue("action").RawValue }))
    {

}

4 个答案:

答案 0 :(得分:3)

您需要将其包含在using语句中。 BeginForm直接写入响应流,它不打算像其他帮助器一样返回字符串。你正在使用它的方式,它正在写它的输出,然后返回MvcForm对象,当它被处置时,它将写入结束形式标记。相反,您的语法强制它在返回的对象上调用ToString,从而产生您看到的输出。

@using(Html.BeginForm(...
{
   ...
}

编辑:由于无法将其包装在using语句中,因此您需要使用HTML显式关闭表单或稍后调用EndForm。在任何一种情况下,您都必须在代码块中使用BeginForm(和EndForm),而不是将其与字符串输出语法一起使用。这两种方法都直接写入响应流。

@{ Html.BeginForm() ..  }

</form>  or   @{ Html.EndForm() }

以下是那些认为我错了(未经许可)的代码的相关部分:

public static void EndForm(this HtmlHelper htmlHelper) {
    HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
    httpResponse.Write("</form>");  // <-- look ma, Response.Write()
}

private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) {
    TagBuilder tagBuilder = new TagBuilder("form");
    tagBuilder.MergeAttributes(htmlAttributes);
    // action is implicitly generated, so htmlAttributes take precedence.
    tagBuilder.MergeAttribute("action", formAction);
    // method is an explicit parameter, so it takes precedence over the htmlAttributes.
    tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);

    HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
    httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag)); <-- and here
    return new MvcForm(htmlHelper.ViewContext.HttpContext.Response);
}

答案 1 :(得分:1)

BeginForm/EndForm助手不会返回IHtmlResult,因此您应该像这样使用它们:

@{ Html.BeginForm(new { action = ViewContext.Controller.ValueProvider.GetValue("action").RawValue }); }

    ...

@{ Html.EndForm(); }

答案 2 :(得分:0)

尝试这样的事情:

    @using (Html.BeginForm()) { 
    @Html.ValidationSummary(true)
    <fieldset>
        <p>
            @Html.LabelFor(model => model.Username) <br />
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </p>

        <p>
            @Html.LabelFor(model => model.Password) <br />
            @Html.EditorFor(model => model.Password) @Html.RouteLink("Forgot Password", "password") <br />
            @Html.ValidationMessageFor(model => model.Password)
        </p>

        <p class="mtop10">              
            <div class="float-left small blue super-button">
                <a class="auto-submit" href="#">Login</a>
            </div>
            <div class="alt-action">
                or @Html.RouteLink("Register", "register")
            </div>
        </p>

    </fieldset>
}

答案 3 :(得分:0)

使用时我也看到System.Web.Mvc.Html.MvcForm

Html.BeginForm("Index", "Home", FormMethod.Post)

以下更改修复了它:

Html.BeginForm("Index", "Home", "POST");

似乎FormMethod.Post强制了不需要的输出,但我无法解释它。这个改变对我有用。

相关问题