可编辑div的html帮助器

时间:2015-08-18 05:00:47

标签: c# asp.net-mvc-4 html-helper

我正在尝试为可编辑的div创建一个html帮助器。我几乎就在那里,因为当我渲染视图时,我在viewsource上看到了html标记,但在视图页面上没有。这里出了点问题。请帮忙,有什么不对或我在这里缺少什么...

C#代码

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;
    using System.Web;
    using System.Web.Routing;

    //namespace AnAReporting.HtmlHelpers
    namespace System.Web.Mvc.Html
    {
        public static class EdiatableDivExtensions
        {
            public static string EdiatableDiv(this HtmlHelper helper, string target, string text)
            {
                return String.Format("<div for='{0}'>{1}</div>", target, text);
            }

            public static MvcHtmlString EdiatableDivFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format, object htmlAttributes)
            {
                return htmlHelper.EdiatableDivFor(expression, format: format, htmlAttributes: HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            }


            public static MvcHtmlString EdiatableDivFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format, IDictionary<string, object> htmlAttributes)
            {
                ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
                return EdiatableDivHelper(htmlHelper,
                                     metadata,
                                     metadata.Model,
                                     ExpressionHelper.GetExpressionText(expression),
                                     format,
                                     htmlAttributes);
            }

            private static MvcHtmlString EdiatableDivHelper(this HtmlHelper htmlHelper, ModelMetadata metadata, object model, string expression, string format, IDictionary<string, object> htmlAttributes)
            {
                return InputHelper(htmlHelper,
                                   InputType.Text,
                                   metadata,
                                   expression,
                                   model,
                                   useViewData: false,
                                   isChecked: false,
                                   setId: true,
                                   isExplicitValue: true,
                                   format: format,
                                   htmlAttributes: htmlAttributes);
            }

            // Helper methods

            private static MvcHtmlString InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary<string, object> htmlAttributes)
            {
                string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
                if (String.IsNullOrEmpty(fullName))
                {
                    throw new ArgumentNullException();
                }

                TagBuilder tagBuilder = new TagBuilder("div");
                tagBuilder.MergeAttributes(htmlAttributes);
                tagBuilder.MergeAttribute("contenteditable", "true");
                tagBuilder.MergeAttribute("name", fullName, true);

                string valueParameter = htmlHelper.FormatValue(value, format);
                bool usedModelState = false;

                //switch (inputType)
                //{
                //    case InputType.CheckBox:
                //        bool? modelStateWasChecked = htmlHelper.GetModelStateValue(fullName, typeof(bool)) as bool?;
                //        if (modelStateWasChecked.HasValue)
                //        {
                //            isChecked = modelStateWasChecked.Value;
                //            usedModelState = true;
                //        }
                //        goto case InputType.Radio;
                //    case InputType.Radio:
                //        if (!usedModelState)
                //        {
                //            string modelStateValue = htmlHelper.GetModelStateValue(fullName, typeof(string)) as string;
                //            if (modelStateValue != null)
                //            {
                //                isChecked = String.Equals(modelStateValue, valueParameter, StringComparison.Ordinal);
                //                usedModelState = true;
                //            }
                //        }
                //        if (!usedModelState && useViewData)
                //        {
                //            isChecked = htmlHelper.EvalBoolean(fullName);
                //        }
                //        if (isChecked)
                //        {
                //            tagBuilder.MergeAttribute("checked", "checked");
                //        }
                //        tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue);
                //        break;
                //    case InputType.Password:
                //        if (value != null)
                //        {
                //            tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue);
                //        }
                //        break;
                //    default:
                //        string attemptedValue = (string)htmlHelper.GetModelStateValue(fullName, typeof(string));
                //        tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(fullName, format) : valueParameter), isExplicitValue);
                //        break;
                //}

                //usedModelState = true;
                tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue);

                if (setId)
                {
                    tagBuilder.GenerateId(fullName);
                }

                // If there are any errors for a named field, we add the css attribute.
                ModelState modelState;
                if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState))
                {
                    if (modelState.Errors.Count > 0)
                    {
                        tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                    }
                }

                tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));

                //if (inputType == InputType.CheckBox)
                //{
                //    // Render an additional <input type="hidden".../> for checkboxes. This
                //    // addresses scenarios where unchecked checkboxes are not sent in the request.
                //    // Sending a hidden input makes it possible to know that the checkbox was present
                //    // on the page when the request was submitted.
                //    StringBuilder inputItemBuilder = new StringBuilder();
                //    inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));

                //    TagBuilder hiddenInput = new TagBuilder("input");
                //    hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
                //    hiddenInput.MergeAttribute("name", fullName);
                //    hiddenInput.MergeAttribute("value", "false");
                //    inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing));
                //    return MvcHtmlString.Create(inputItemBuilder.ToString());
                //}

                //return tagBuilder.ToMvcHtmlString(TagRenderMode.SelfClosing);

                StringBuilder inputItemBuilder = new StringBuilder();
                inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));

                //TagBuilder divInput = new TagBuilder("div");
                //divInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
                //divInput.MergeAttribute("name", fullName);
                //divInput.MergeAttribute("value", "false");
                //inputItemBuilder.Append(divInput.ToString(TagRenderMode.SelfClosing));
                return MvcHtmlString.Create(inputItemBuilder.ToString());
            }

            private static RouteValueDictionary ToRouteValueDictionary(IDictionary<string, object> dictionary)
            {
                return dictionary == null ? new RouteValueDictionary() : new RouteValueDictionary(dictionary);
            }



        }
    }

从视图中调用html帮助器...

          @Html.EdiatableDivFor(model => model.Remarks, null, new { style =                                      "display: block;"})

网页浏览源...

<div contenteditable="true" data-val="true" data-val-length="Minumum 25 and maximum 200 characters are Allowed" data-val-length-max="200" data-val-length-min="25" data-val-required="Please provide Remarks" id="Remarks" name="Remarks" style="display: block;" value="[TECH. REMARKS]: REPORT CASE45RT65CASE45RT65**[VERIFIER REMARKS]: REPORT REJECTED BY THE VERIFIER***[TECH. REMARKS]: REPORT MODIFIED BY TECHNICIAN**[JOB VERIFIED]" />

0 个答案:

没有答案