MVC 用擴充方法執行自訂視圖,替代 UIHint

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   strong   

項目中用了 Bootstrap , 這樣就不用寫太多的CSS了,省去很多事情。
但是這個業務系統需要輸入的地方很多,每個表都有100多個欄位,每個頁面需要大量的表單。
把這些表單按 bootstrap 的格式寫出來,也是件頭痛的事情。
我想到模板,EditorTemplates UIHint, 但是 UIHint 需要用 Metadata 標註,一個一個的加,也是不現實的。
還有別外一種辦法,就是擴充 HtmlHelper。
要用HtmlHelper ,大家可能就想到了 TagBuilder 了,TagBuilder 基本全是 Hard code 了,不方便調整顯示格式。

最終我用了另外一種辦法:
在 HtmlHelper 擴充裡,取自訂的視圖,視圖可以隨時改,又不用每個欄位去加 UIHint.

 1 public static MvcHtmlString EditorBlockAFor<TModel, TProperty>(this HtmlHelper<TModel> helper, string template, Expression<Func<TModel, TProperty>> property, bool withLabel, string containerClass = "col-xs-4", object htmlAttributes = null) { 2  3     var body = (MemberExpression)property.Body; 4     if (body == null) 5         throw new ArgumentException(); 6  7     var ctx = helper.ViewContext.Controller.ControllerContext; 8     var result = ViewEngines.Engines.FindPartialView(helper.ViewContext.Controller.ControllerContext, template); 9     if (result.View != null) {10 11         var metadata = ModelMetadata.FromLambdaExpression(property, helper.ViewData);12         //var model = metadata.Model;13 14         var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);15 16         using (var writer = new StringWriter(CultureInfo.CurrentCulture)) {17             var vctx = new ViewContext(ctx, result.View, helper.ViewData, helper.ViewContext.TempData, writer);18             vctx.ViewBag.PropertyName = string.Join(".", body.ToString().Split(new char[] { ‘.‘ }).Skip(1));//body.Member.Name;19 20             vctx.ViewBag.ContainerClass = containerClass;21             vctx.ViewBag.IsRequired = metadata.IsRequired;22             vctx.ViewBag.HtmlAttributes = attrs;23             vctx.ViewBag.WithLabel = withLabel;24 25             vctx.ViewBag.DisplayName = metadata.DisplayName ?? metadata.PropertyName;26 27             result.View.Render(vctx, writer);28 29             return MvcHtmlString.Create(writer.ToString());30         }31     } else {32         throw new InvalidOperationException(string.Format("particle view {0} not found", template));33     }34 }

 

這個是核心,其它的擴充都是調用這個方法,比如:

1 public static MvcHtmlString TextBlockFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> property, string containerClass = "col-lg-4 col-md-4 col-xs-4", object htmlAttributes = null, bool withLabel = true) {2     return EditorBlockAFor(helper, "TextBlock", property, withLabel, containerClass, htmlAttributes);3 }

 

在 EditorBlockAFor 這個方法裡,有一句:
var result = ViewEngines.Engines.FindPartialView(helper.ViewContext.Controller.ControllerContext, template);
這個是去尋找指定的自訂視圖,它就是整個思路的關鍵。

自訂視圖 TextBlock.cshtml

@model object@{    this.Layout = null;    string propertyExpression = ViewBag.PropertyName;    string containerClass = ViewBag.ContainerClass;    RouteValueDictionary htmlAttributes = SharedTemplatesHelper.MargeClass(ViewBag);}<div class="@containerClass">    @SharedTemplatesHelper.Label(ViewBag)    @Html.TextBox(propertyExpression, null, htmlAttributes)</div>

注意,模型是object , 因為不確定模型的類型。

 

這個檔案需要放到 Shared 下,這個是簡單的結構。

調用:
@Html.TextBlockFor(m => m.VesselInfo.VESSEL_NAME_CN, "col-lg-2 col-md-2")
最終會產生這樣一段HTML:

<div class="col-lg-2 col-md-2">            <span class="help-block">中文名稱         <span class="red">*</span></span>    <input class="form-control input-sm" data-val="true" data-val-length="The field 中文名稱 must be a string with a maximum length of 500." data-val-length-max="500" data-val-required="The 中文名稱 field is required." id="VesselInfo_VESSEL_NAME_CN" name="VesselInfo.VESSEL_NAME_CN" type="text" value="" /></div>

 

長這個樣子:

 

 

--------------分隔線內是廢話,有興趣可以瞭解一下我的崩潰經曆-------------------

一切都按照設想的樣子,直到。。。。
有一天,同事說明明是 Required 的,為什麼沒有執行驗證?
我簡單的看了一下,是因為沒有產生 data-xxx 這樣的驗證屬性。看了一下 Action ,就是 Return View(); 沒有傳遞 model 到視圖。
聲明了一個 model 傳給視圖 (return View(XXX);) 後,一切正常。
我做MVC3的時候,不給 Model 都會輸出驗證屬性,當時趕時間,沒有去深入研究為什麼,還以為 MVC 5 的新特性呢。

國慶過7天豬一樣的生活,項目也進行的七七八八了,終於有時間回頭看看了。
下了MVC的最新源碼:
http://aspnetwebstack.codeplex.com/SourceControl/latest
版本號碼是 5.2.3.0, 我們項目中用的是 5.2.0.0 ,差別不大。

建立了一個測試專案,
編譯了一份MVC的DLL,連同PDB一起放到項目的引用目錄下,改了一下MVC的配置、引用,在 InputExtensions 下加了斷點,但是卻無法斷點。
按照網上的搜到的調試 MVC 源碼的方法去做,很不幸,沒有一個適用的。

搞到晚上8點多,還是沒有辦法調試進源碼。真是崩潰至極了。
又找到了篇 pdb 符號伺服器的博文:
http://weblogs.asp.net/gunnarpeipman/stepping-into-asp-net-mvc-source-code-with-visual-studio-debugger
但是需要下載這些符號,電腦沒關,回去了。

今天按照博文的說明去做,仍然不能調試進源碼。


很有幸,找到另外一篇文:
http://blogs.msdn.com/b/micl/archive/2014/06/07/how-to-debug-your-code-with-mvc-fresh-source-code.aspx

Before each version of MVC launch, the contribute team always strong name each MVC related assembly by a specified keyfile 35MSSharedLib1024.snk which is located in tools folder to prevent assembly tamper. But the snk file that you get doesn‘t contain private key, that you can only delay signed all assemblies if you compile directly. Unfortunately, delay signed assembly doesn‘t support debug feature.
簡單的翻譯一下:
因為我們下載到的 MVC 源碼是經過簽名的,源碼裡提供的密鑰不包含私密金鑰,只能是延遲簽名。很不幸,延遲答名是不能DEBUG 的。
(這方面我沒有經驗,不懂)

按照博文的做法,將相關的項目都去掉了簽名:
System.Web.Helpers
System.Web.Mvc
System.Web.Razor
System.web.WebPages
等

然後將 System.Web.WebPages 下的
AssemblyInfo.cs (在 Properties 下) 中的 InternalsVisibleTo 參數換成不帶版本號碼的。
[assembly: InternalsVisibleTo("System.Web.Mvc")]
[assembly: InternalsVisibleTo("System.Web.Helpers")]

編譯,修改測試專案的的 web.config ,將 System.Web.Mvc 的版本改成編譯的版本號碼,刪除原來的相關引用,添加為剛剛編譯過的相關DLL
加斷點,運行,調試進去了。

--------------分隔線-------------------

 

上面都是廢話。
調了一圈,發現在 ModelMetadata.cs 第 382 行:
ModelMetadata propertyMetadata = viewData.ModelMetadata.Properties.Where(p => p.PropertyName == expression).FirstOrDefault();
匹配不到 property, propertyMetadata 的結果是 null.
屬性明明是有的,就死活就是沒有屬性的 Metadata.

當快速監視 viewData.ModelMetadata 後,這個值又有了,說明問題出在這個 viewData.ModelMetadata 上。

在 ViewDataDictionary.cs ,定義的是 ViewDataDictionary , 第82行,
if (_modelMetadata == null && _model != null)
_model 無疑是傳到視圖裡的 model
當 _modelMetadata = null 且 _model 不為 null 的時候,會去模數型的Metadata ,這就解釋了為什麼當傳 Model 到視圖的時候,會輸出驗證屬性了。
當是不傳 Model 到視圖,就返回 null 了。

進入 ViewDataDictionaryOfModel.cs
定義的是 ViewDataDictionary<TModel> ,繼承自 ViewDataDictionary
在 第35行,取父類的 ModelMetadata, 因為返回的是 null, 又去按 模型(Model) 的類型去取 Metadata.

接上面的自訂視圖,TextBlock.cshtml
@model object
...
...

指定的模型類型是 object , 這是因為它是 Shared 的,不確定模型的確切類型,所以我用了 object.
好了,問題來了,挖掘機哪家強?按 object 去取 Metadata 當然是取不到的!

轉了一轉,又把我逼到當初處理這個東西的原點上。
想到快速監視後,是可以取到想要的結果的,我把上面的擴充方法改加一句:

。。。
var vctx = new ViewContext(ctx, result.View, helper.ViewData, helper.ViewContext.TempData, writer);
vctx.ViewData.ModelMetadata = helper.ViewData.ModelMetadata;//////必須的,調了很長時間,定位問題在這個 ModelMetadata 上
。。。

運行,通過!

MVC 用擴充方法執行自訂視圖,替代 UIHint

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.