は、それをドロップすると、それが動作持って探している人のために上記のチャドの答えにhtmlAttributes過負荷を追加したいです。他のすべてのヘルパーは、これらの例から簡単に構築できます。 (ありがとうチャド、あなたの拡張機能はノックアウトの使用への私の移行を容易にしました!)
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;
}
}
}
http://knockoutmvc.com/について知っていますか?私はそれについて悪いことを聞いてきましたが、http://stackoverflow.com/questions/11618042/is-there-a-reason-i-would-use-knockout-mvc-instead-of-knockout-js。しかし、おそらくあなたはそれからいくつかの拡張を盗むことができるでしょう:) –
私はその感謝を見てください –
私はMVC3でこれを戻すために同様のプロジェクトを開始しましたが、私たちはMVC4でノックアウトの統合とシングルページアプリケーションのテンプレートそれに取り組むことをやめた。残念ながら、これは実現しておらず、プロジェクトに戻ったことはありません。今、私はその機能がSP2で返されている(または返されている)と確信しています。 –