gpt4 book ai didi

css - 如何将类添加到 Razor 形式并提供填充?

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:53 25 4
gpt4 key购买 nike

 @using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.Name, new { @Value = "Name" })
@Html.TextBoxFor(m => m.Email, new { @Value = "Email" })
<input type="submit" value="Go" />
}

我如何向其中添加一个 css 类并添加填充?我有以下 css 我想添加以尝试填充它一点

    .newsletterform
{
color:black;
padding-left:20px;
}

最佳答案

您可以使用 @class="className" 设置类,但您还必须指定 actionName、controllerName、FormMethod,因为没有重载 Html.BeginForm支持只设置html属性。

@Html.BeginForm("ActionName", "ControllerName", 
FormMethod.Post, new { @class = "newsletterform" }

您可以创建自己的 html 帮助程序,它也会为您做这些。

更新

这是一个自定义的 html 帮助程序。

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 });
}
}

您可以像这样从您的 View 中调用该方法。

@using (Html.BeginFormWithClassName("newsletterform"))
{

}

希望对您有所帮助

关于css - 如何将类添加到 Razor 形式并提供填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8429553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com