Asp.net Mvc Display template of String, but now every simple type wants to use it!

风流意气都作罢 提交于 2019-12-21 11:34:33

问题


I created a Display Template which when passed a string renders a disabled text box

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>
<%: Html.TextBoxFor(model => model, new { disabled = "disabled" })%>

Which works great. However, for some reason MVC wants to try and stuff DateTimes and Ints through it as well, which is throwing exceptions

The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.

Any ideas?


回答1:


You don't need to strongly type the template to a String.

you can try something like this :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
             , new { disabled = "disabled" }) %>

And in your view you call it like this

Html.DisplayModelFor(model => mode.name);

For more information see an example of the default built-in editor template for the string in Brad Wilson article in his his series on Templates in ASP.NET MVC.

You should consider going through the complete series. I can't express how helpful this series was for me.



来源:https://stackoverflow.com/questions/3928134/asp-net-mvc-display-template-of-string-but-now-every-simple-type-wants-to-use-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!