简体   繁体   English

ASP.NET MVC帮助器,为模型的两个属性创建帮助器

[英]ASP.NET MVC Helpers, create helpers for two properties of model

I need create helpers for two properties of model For example 我需要为模型的两个属性创建帮助器,例如

@Html.TestFor(m => m.Item1, m=>m.Item2)

And so how can write method for this helper? 那么如何为该助手编写方法呢?

For example 例如

public static MvcHtmlString TestFor<TModel, TValue>
          (this HtmlHelper<TModel> self,
          Expression<Func<TModel, TValue>> expression)
        {
...
}

It is method for only one property of model. 是仅针对模型的一种性质的方法。 How can write for few property? 怎么写很少的财产? Thank for ideas 谢谢你的想法

You can just use multiple expressions in your helper: 您可以在助手中使用多个表达式:

public static MvcHtmlString TestFor<TModel, TValue>
          (this HtmlHelper<TModel> self,
          Expression<Func<TModel, TValue>> expression, Expression<Func<TModel, TValue>> secondExpression)
{
...
}

or use params to indicate multiple expressions and work with array of expressions later: 或使用params指示多个表达式并稍后使用表达式数组:

public static MvcHtmlString TestFor<TModel, TValue>
          (this HtmlHelper<TModel> self,
          params Expression<Func<TModel, TValue>>[] expressions)
{
...
}

if property types different each other use like this : 如果属性类型互不相同,则使用以下代码:

public static MvcHtmlString ImageFor<TModel, TProperty, KProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expressionImageProperty,//this is HttpPostedFile
            Expression<Func<TModel, KProperty>> expressionImageIdProperty,//this property is ID for image that store in database
            object filehtmlAttributes, object imageBoxHtmlAttributes)//html attributes for html controls
        {
            var sb = new StringBuilder();
            var fileattr = new RouteValueDictionary(filehtmlAttributes);
            if (!fileattr.ContainsKey("id"))
                fileattr.Add("id", string.Format("ImageForFile_{0}", ((MemberExpression)expressionImageProperty.Body).Member.Name));
            fileattr.Add("type", "file");
            if (!fileattr.ContainsKey("class"))
                fileattr.Add("class", "form-control");
            fileattr.Add("onchange", "readURL(this);");
            sb.Append((string)htmlHelper.TextBoxFor<TModel, TProperty>(expressionImageProperty, fileattr).ToHtmlString());

            TagBuilder tagImg = new TagBuilder("img");
            tagImg.Attributes.Add("id", string.Format("ImageForFileViewer_{0}", ((MemberExpression)expressionImageProperty.Body).Member.Name));

            if (htmlHelper.ViewData.Model == null)
                tagImg.Attributes.Add("src",
                    new UrlHelper(HttpContext.Current.Request.RequestContext).Action("GetImage", "ImageManagement",
                        new { imageId = "0" }));
            else
            {
                var modelImageId = expressionImageIdProperty.Compile()(htmlHelper.ViewData.Model).ToString();
                if (string.IsNullOrEmpty(modelImageId))
                    modelImageId = "0";
                tagImg.Attributes.Add("src",
                    new UrlHelper(HttpContext.Current.Request.RequestContext).Action("GetImage", "ImageManagement", //ImageManagementController get FileResult
                        new
                        {
                            imageId = modelImageId,thumb=true,size="small"
                        }));
            }

            if (imageBoxHtmlAttributes != null)//
                tagImg.MergeAttributes(new RouteValueDictionary(imageBoxHtmlAttributes));//

            sb.Append(tagImg.ToString(TagRenderMode.Normal));

            sb.Append((string)htmlHelper.HiddenFor<TModel, KProperty>(expressionImageIdProperty).ToHtmlString()); //for update

            sb.AppendLine(
               EngineContext.Current.Resolve<IPageScriptManager>()//this is simple mvc script manager resolved from IOC
                                 .ScriptInclude<CommonResourceAccessor>("imageFor" + "_script",
                                                                        "***.WebFramework.Resources.ImageFor.js")//if you have some js file add as resource
                                 .Render().ToString());
            return MvcHtmlString.Create(sb.ToString());
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM