ASP.NET MVC 2 - Html.EditorFor可空类型? [英] ASP.NET MVC 2 - Html.EditorFor a nullable type?

查看:193
本文介绍了ASP.NET MVC 2 - Html.EditorFor可空类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个编辑模板:一个小数,一个用于小数? (可为空)

但是,当我在我的模型可空的小数,它会尝试加载正常的十进制编辑:

 <%:Html.EditorFor(型号=> model.SomeDecimal)%GT;
<%:Html.EditorFor(型号=> model.SomeNullableDecimal)%GT;

第一个工作正常,并加载小数编辑模板。第二个也尝试加载小数模板(和失败,因为它不是一个小数字段)。

错误消息是:

 模型项目传递到字典是空的,但本词典需要
类型的非空模型项目System.Decimal。

我的模板声明如下:

十进制模板:

 <%@控制语言=C#
继承=System.Web.Mvc.ViewUserControl&所述; System.Decimal>中%GT;

可空小数模板:

 <%@控制语言=C#
继承=System.Web.Mvc.ViewUserControl< System.Decimal>? %GT;

我知道我可以把它通过传递模板名称的工作,如:

不过,我真的preFER它只是使用类型就像所有其他模板自动工作。

 <%:Html.EditorFor(型号=> model.SomeNullableDecimal,
NullableDecimalTemplate)%>


解决方案

感谢布莱恩添加赏金,试图得到一个积极的解决办法,但我不得不回答说,我已经找到了答案绝对不 - 你不能从它的类型可为空的模板自动发现。您必须使用模板名称。

这是布拉德·威尔逊的博客的相关报价在<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html\">http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html.他是MVC的权威来源,所以我不得不相信他时,他说:


  

当搜索的类型名称,
  简单的名字被使用(即Type.Name)
  没有命名空间。此外,如果类型
  可以为空,我们搜索T(所以
  你会得到布尔模板
  无论你使用布尔或
  可空)


他还继续说:


  

这意味着如果你正在写模板
  值类型,则需要
  帐户的值是否为
  可为空或没有。您可以使用
  的IsNullableValueType财产
  ModelMetadata以确定是否
  值可为空。我们将看到一个
  的本实施例中下面的
  内置布尔模板


所以,是有一个回答这个问题,但不幸的是,答案是否定的。

要使用一个可以为空模板,您必须显式使用的模板名称:

 &LT;%:Html.EditorFor(型号=&GT; model.SomeNullableDecimal,NullableDecimalTemplate)%&GT;

或者你可以用一个模板同时处理可空和非可空类型:

 &LT;若%(ViewData.ModelMetadata.IsNullableValueType){%GT;
    &LT;%= Html.DropDownList(,TriStateValues​​,新{@class =列表框三态})%GT;
&LT;%}其他{%GT;
    &LT;%= Html.CheckBox(,价值??假的,新的{@class =复选框})%GT;
&LT;%}%GT;

I have two editor templates: one for decimal, and one for decimal? (nullable)

But when I have a nullable decimal in my model, it tries to load the normal decimal editor:

<%: Html.EditorFor(model => model.SomeDecimal )%>
<%: Html.EditorFor(model => model.SomeNullableDecimal )%>

The first one works fine, and loads the decimal editor template. The second one also tries to load the decimal template (and fails because it is not a decimal field).

The error message is:

The model item passed into the dictionary is null, but this dictionary requires 
a non-null model item of type 'System.Decimal'. 

My templates are declared like this:

Decimal template:

<%@ Control Language="C#" 
Inherits="System.Web.Mvc.ViewUserControl<System.Decimal>" %>

Nullable Decimal template:

<%@ Control Language="C#" 
Inherits="System.Web.Mvc.ViewUserControl<System.Decimal?>" %>

I know that I can make it work by passing in the template name, eg

But I would really prefer it to just work automatically by using the type just like all the other templates.

<%: Html.EditorFor(model => model.SomeNullableDecimal, 
"NullableDecimalTemplate" )%>

解决方案

Thanks to Bryan for adding a bounty to try to get a positive solution, but I'm going to have to answer and say that I have found that the answer is definitely NO - you cannot have a nullable template auto-discovered from its type. You must use a template name.

This is the relevant quote from Brad Wilson's blog at http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html. He is an authoritative source on MVC so I have to believe him when he says:

When searching for the type name, the simple name is used (i.e., Type.Name) without namespace. Also, if the type is Nullable, we search for T (so you’ll get the Boolean template whether you’re using "bool" or "Nullable")

He also goes on to say

This means if you’re writing templates for value types, you will need to account for whether the value is nullable or not. You can use the IsNullableValueType property of ModelMetadata to determine if the value is nullable. We’ll see an example of this below with the built-in Boolean template.

So YES there is an answer to this question, but unfortunately the answer is NO.

To use a nullable template you must explictly use the template name:

<%: Html.EditorFor(model => model.SomeNullableDecimal, "NullableDecimalTemplate" )%>

Or you can use one template that handle both the nullable and the non nullable type:

<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
    <%= Html.DropDownList("", TriStateValues, new { @class = "list-box tri-state" })%>
<% } else { %>
    <%= Html.CheckBox("", Value ?? false, new { @class = "check-box" })%>
<% } %>

这篇关于ASP.NET MVC 2 - Html.EditorFor可空类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆