Css 如何将类添加到razor表单并提供填充?
您可以使用Css 如何将类添加到razor表单并提供填充?,css,asp.net-mvc-3,razor,Css,Asp.net Mvc 3,Razor,您可以使用@class=“className”设置类,但也必须指定actionName、controllerName和FormMethod,因为不存在只支持设置Html属性的Html.BeginForm重载 .newsletterform { color:black; padding-left:20px; } 您可以创建自己的html助手,这也可以为您实现 更新 这里有一个自定义的html帮助程序可以实现这一点 @Html.BeginForm("ActionName",
@class=“className”
设置类,但也必须指定actionName、controllerName和FormMethod,因为不存在只支持设置Html属性的Html.BeginForm
重载
.newsletterform
{
color:black;
padding-left:20px;
}
您可以创建自己的html助手,这也可以为您实现
更新
这里有一个自定义的html帮助程序可以实现这一点
@Html.BeginForm("ActionName", "ControllerName",
FormMethod.Post, new { @class = "newsletterform" }
您可以像这样从视图中调用该方法
public static class HtmlHelperExtension
{
public static MvcForm BeginFormWithClassName(this HtmlHelper helper, string cssClassName)
{
string controllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string actionName = (string)helper.ViewContext.RouteData.Values["action"];
return helper.BeginForm(actionName, controllerName, FormMethod.Post, new { @class = cssClassName });
}
}
希望这有帮助我曾尝试使用此帮助程序在web.config的节中添加其名称空间,但运气不佳,您是否能够全局使用该帮助程序?谢谢您可以将扩展方法放入
名称空间System.Web.Mvc
public static class HtmlHelperExtension
{
public static MvcForm BeginFormWithClassName(this HtmlHelper helper, string cssClassName)
{
string controllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string actionName = (string)helper.ViewContext.RouteData.Values["action"];
return helper.BeginForm(actionName, controllerName, FormMethod.Post, new { @class = cssClassName });
}
}
@using (Html.BeginFormWithClassName("newsletterform"))
{
}