基于变量类型

时间:2015-05-29 04:06:09

标签: asp.net asp.net-mvc razor

我有一个利用ASP.NET MVC和Razor页面布局的项目。相关页面将是一个调查,其问题,数据类型和答案已由管理员用户配置并从数据库中检索。例如:

public class ExampleViewModel 
{
    //the user define dquestion
    public string Question1Text { get; set; }

    //this is an enum with "Text","YesNo","DropDown"
    public AnswerType Question1Type { get; set; } 

    //this would hold options for the drop down list
    public string Question1Options { get; set; }

    //the user input answer
    public string Question1Answer { get; set; }
}

我不确定如何构造Razor视图以根据AnswerType创建适当类型的表单输入字段。我似乎记得有关为各种DataType()注释创建模板的内容,但我不知道从哪里开始查看,如果这适用于这种情况?

1 个答案:

答案 0 :(得分:1)

你想使用模板助手 - 这是一个很好的演练 - http://www.hanselman.com/blog/ASPNETMVCDisplayTemplateAndEditorTemplatesForEntityFrameworkDbGeographySpatialTypes.aspx

在助手本身,您可以执行以下操作:

@if (model.AnswerType is xxx)
{
     <button> xxx </button> - or your html
}