ASP.NET MVC-在自定义HTML帮助器中使用模型 [英] ASP.NET MVC - Using Model in Custom HTML Helper

查看:61
本文介绍了ASP.NET MVC-在自定义HTML帮助器中使用模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC和Bootstrap构建应用程序.在我的应用中,我的模型视图如下:

I am building an app with ASP.NET MVC and Bootstrap. In my app, I have a view with a Model that looks like this:

public class EntryModel
{
  [Required(ErrorMessage="Please enter the name.")]
  [Display(Name="Name")]
  public string Name { get; set; }

  [Required (ErrorMessage="Please enter the description.")]
  [Display(Name = "Description")]
  public string Description { get; set; }
}

在此应用中,我还定义了一个自定义html帮助器,如下所示:

In this app, I've also defined a custom html helper that looks like this:

public static class MyHelpers
{
  public static MvcHtmlString MyTextBox(this HtmlHelper helper)
  {
    var sb = new StringBuilder();
    sb.Append("<div class=\"form-group\">");
    sb.Append("<label class=\"control-label\" for=\"[controlId]\">[labelValue]</label>");
    sb.Append("<input class=\"form-control\" id=\"[controlId]\" name=\"controlId\" type=\"text\" value=\"[propertyValue]\">");
    sb.Append("</div>");

    return MvcHtmlString.Create(sb.ToString());
  }
}

我正在Razor视图中使用此帮助程序和模型,如下所示:

I'm using this helper and model in my Razor view, like this:

@model EntryModel
<h2>Hello</h2>

@using (Html.BeginForm("Add", "Entry", new {}, FormMethod.Post, new { role="form" }))
{
  @Html.MyTextBox() 
}

我试图根据模型的属性在帮助器中生成labelValuecontrolIdpropertyValue值.例如,我想使用@Html.MyTextBoxFor(m => m.Name)并让帮助程序生成如下内容:

I am trying to generate the labelValue, controlId, and propertyValue values in the helper from the properties of the Model. For example, I'd like to use @Html.MyTextBoxFor(m => m.Name) and have the helper generate something like this:

<div class="form-group">
  <label class="control-label" for="Name">Name</label>");
  <input class="form-control" id="Name" name="Name" type="text" value="Jon">
</div>

基本上,我不确定如何将模型信息放入html帮助器中.

Essentially, I'm not sure how to get my model information into my html helper.

推荐答案

使用以下示例作为参考:

Use this example as reference:

        public static MvcHtmlString AutoSizedTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {     
          var attributes = new Dictionary<string, Object>();
          var memberAccessExpression = (MemberExpression)expression.Body;
          var stringLengthAttribs =  memberAccessExpression.Member.GetCustomAttributes(
            typeof(System.ComponentModel.DataAnnotations.StringLengthAttribute), true);

         if (stringLengthAttribs.Length > 0)
         {
            var length = ((StringLengthAttribute)stringLengthAttribs[0]).MaximumLength;

            if (length > 0)
            {
                attributes.Add("size", length);
                attributes.Add("maxlength", length);
            }
        }

        return helper.TextBoxFor(expression, attributes);
    }

您可以像这样在视图中调用它: @ Html.AutoSizedTextBoxFor(x => x.Address2)

And you can call it in the view like this: @Html.AutoSizedTextBoxFor(x => x.Address2)

这篇关于ASP.NET MVC-在自定义HTML帮助器中使用模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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