Html扩展方法无法在表单中工作

Bac*_*ave 1 html c# asp.net asp.net-mvc-5

我有以下表格:

@using (Html.BeginForm("Cancel", "Member", FormMethod.Post))
{        
    Html.SubmitButton(); //doesn't work
}
@*@Html.SubmitButton(); //does work*@
Run Code Online (Sandbox Code Playgroud)

扩展方法如下所示:

public static MvcHtmlString SubmitButton(this HtmlHelper helper)
{
    return new MvcHtmlString("<input type = \"submit\" value = \"Cancel\" />");
}
Run Code Online (Sandbox Code Playgroud)

为什么输入元素HTML能够在使用块之外但不在其中生成?

小智 5

你错过了@告诉剃刀将结果输出到视图的前导.当您使用just时Html.SubmitButton();,将执行该方法并返回结果,但不会执行任何操作.

你的代码需要

@using (Html.BeginForm("Cancel", "Member", FormMethod.Post))
{        
    @Html.SubmitButton()
}
Run Code Online (Sandbox Code Playgroud)