EditorFor HTML Helper with knockout



我认为有一个扩展版本的EditorFor HTML帮助器,它自动为Knockout JS编写值数据绑定。

这将用于客户端视图模型和服务器端视图模型相同的地方-我已经通过使用ko映射和通过AJAX获得视图模型自动生成了客户端视图模型。

有没有其他人尝试过这样的东西,或者有任何项目包括类似于我在这里想的东西?

这样做的好处是,在重构时,不会有丢失数据绑定值的危险。

我们已经按照这些思路做了一些事情,它远非完美,我们在自定义扩展中还有更多,但我提取了精华。

using System.Web.Mvc.Html;
namespace System.Web.Mvc
{
    public static class EditorForExtensions
    {
        public static MvcHtmlString TextBoxForViewModel<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var htmlAttributes = HtmlAttributesForKnockout(metadata);
            return htmlHelper.TextBoxFor(expression, htmlAttributes);
        }
       private static Dictionary<string, object> HtmlAttributesForKnockout(ModelMetadata metadata)
        {
            var htmlAttributes = new Dictionary<string, object>();
            var knockoutParameter = String.Format("value: {0}", metadata.PropertyName);
            htmlAttributes.Add("data-bind", knockoutParameter);
            return htmlAttributes;
        }
    }
}

可以这样使用:

@Html.TextBoxForViewModel(m => m.Name)

想把htmlAttributes重载添加到上面Chad的答案中,以供任何希望将其放入并使其工作的人使用。可以很容易地从这些示例构建所有其他帮助程序。(谢谢Chad,你的扩展帮助我过渡到使用knockout!)

using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc.Html;
namespace System.Web.Mvc {
    public static class KnockoutExtensions {
        public static MvcHtmlString KnockoutTextBoxFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression ) {
            var metadata = ModelMetadata.FromLambdaExpression( expression, htmlHelper.ViewData );
            var htmlAttributes = HtmlAttributesForKnockout( metadata );
            return htmlHelper.TextBoxFor( expression, htmlAttributes );
        }
        public static MvcHtmlString KnockoutTextBoxFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object attributes ) {
            // convert passed anonymous object (attributes) into IDictionary<string,object> to pass into attribute parser
            var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes( attributes ) as IDictionary<string, object>;
            var metadata = ModelMetadata.FromLambdaExpression( expression, htmlHelper.ViewData );
            var htmlAttributes = HtmlAttributesForKnockout( metadata, attrs );
            return htmlHelper.TextBoxFor( expression, htmlAttributes );
        }
        private static Dictionary<string, object> HtmlAttributesForKnockout( ModelMetadata metadata, IEnumerable<KeyValuePair<string, object>> attributes = null ) {
            var htmlAttributes = new Dictionary<string, object>();
            var knockoutParameter = String.Format( "value: {0}", metadata.PropertyName );
            htmlAttributes.Add( "data-bind", knockoutParameter );
            if ( attributes != null ) foreach ( var attr in attributes ) htmlAttributes.Add( attr.Key, attr.Value );
            return htmlAttributes;
        }
    }
}

最新更新