如何添加类剃刀形式,给填充? [英] how to add class to razor form and give padding?

查看:119
本文介绍了如何添加类剃刀形式,给填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @using(Html.BeginForm())
 {
   @ Html.TextBoxFor(M = GT; m.Name,新{@Value =姓名})
   @ Html.TextBoxFor(M = GT; m.Email,新{@Value =电子邮件})
   <输入类型=提交值=转到/>
  }

我怎么一个CSS类添加到这一点,并增加填充?我有以下的CSS我要补充试试垫有点

  .newsletterform
{
    颜色:黑色;
    填充左:20像素;
}


解决方案

您可以设置 @类=类名类,但你必须指定a​​ctionName,controllerName ,FormMethod也因为没有 Html.BeginForm 过载,支持设置仅HTML属性。

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

您可以创建自己的HTML帮助,这并不为你了。

更新

下面是一个自定义的HTML帮助做到这一点。

 公共静态类HtmlHelperExtension
{
    公共静态MvcForm BeginFormWithClassName(此的HtmlHelper帮手,串cssClassName)
    {
        字符串controllerName =(字符串)helper.ViewContext.RouteData.Values​​ [控制器];
        字符串actionName =(字符串)helper.ViewContext.RouteData.Values​​ [行动];
        返回helper.BeginForm(actionName,controllerName,FormMethod.Post,新{@class = cssClassName});
    }
}

您可以从您的观点是这样调用该方法。

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

希望这有助于

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

how do i add a css class to this and add padding? I have the following css i wish to add to try pad it a bit

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

解决方案

You can set the class with @class="className" but you have to specify actionName, controllerName, FormMethod too because there is no overload of Html.BeginForm that supports setting only html attributes.

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

You can create your own html helper, that does that for you, too.

Update

Here is a custom html helper does that.

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

You can call the method from your view like this.

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

}

hope this helps

这篇关于如何添加类剃刀形式,给填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆