处理与编辑模板可空类型 [英] Handle Nullable Types with editor template

查看:131
本文介绍了处理与编辑模板可空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个编辑模板至极取布尔类型,并创建一个DropDownList,以改真正默认值放到的Si 。现在,当我部署的应用程序,我没有意识到布尔可空<布尔> 采取像一样并影响了我的编辑模板(boolean.cshtml)的名称。现在,我不想这样,我想修改的行为 editorfor ,只有当该模型的数据类型是可空<布尔> 不能当它是布尔值。我该如何处理这种???

  @model可空<布尔>@ {
    VAR时listItems =新[]
    {
        新SelectListItem {值=真,文本=思},
        新SelectListItem {值=假,文本=否}
    };}
@ Html.DropDownListFor(型号=> model.Value,listItems中)


解决方案

您不能prevent它们使用相同的编辑模板。但是你可以编辑模板内处理。您有:

  ViewData.ModelMetadata.IsNullableValueType

如果你读这篇文章,你可以看到这个默认模板确实给两个不同的效果图之间切换内容。从它只是偷:

<一个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

I developed a editor template wich take boolean types and creates a dropdownlist, to changed the true and false default values, into Si and No. Now when I deployed the application i didn't realized that the boolean and Nullable<boolean> are taken like the same and affected for the name of my editor template (boolean.cshtml). Now I don't want this, I want to modify the behavior of the editorfor, only when the datatype of the model was Nullable<boolean> not when it's boolean. How can I handle this???

 @model Nullable<bool>

@{
    var listItems = new[]
    {
        new SelectListItem { Value = "true", Text = "Si" },
        new SelectListItem { Value = "false", Text = "No" }
    };  

}


@Html.DropDownListFor( model => model.Value, listItems)

解决方案

You can't prevent them using the same editor template. But you can handle it within the editor template. You have:

ViewData.ModelMetadata.IsNullableValueType

If you read this article, you can actually see what the default template does with this to switch between two different renderings. Just steal from it:

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

这篇关于处理与编辑模板可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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