Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 如何将类添加到razor表单并提供填充?_Css_Asp.net Mvc 3_Razor - Fatal编程技术网

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"))
{

}