通过视图模型传递参考/静态数据到MVC3中的视图

问题描述

| 我创建了一个类来保存一些静态数据:
static public class ReferenceData
    {
        [DisplayName(\"Status\")]
        public IEnumerable<SelectListItem> StatusType
        {
            get
            {
                return new[]
                    {
                        new SelectListItem { Value = \"0\",Text = \"Release\" },new SelectListItem { Value = \"1\",Text = \"Beta\" },new SelectListItem { Value = \"2\",Text = \"Alpha\" },};
            }
        }
    }
现在,我想使用该类数据在视图中填充一个下拉列表。我有以下视图模型:
public class adminViewModel
{
    public HouseData   HouseData { get; set; }
    public string Status { get; set; }
    public ReferenceData ReferenceData { get; } // <<<<<< My problem here
}


@Html.DropDownListFor(
  x => x.Status,new SelectList(Model.ReferenceData.StatusType,\"Value\",\"Text\"),new { style = \"display: inline;\" }
)
对我来说,问题是我不确定这是否是一种有效的方法,因为我使用的是静态类,并且在获取get时使用。在{}内部,它在编译期间抱怨,并显示一条错误消息:
static types cannot be used as return types
即使数据始终是静态的,我是否真的需要使其成为非静态的并实例化该类,然后才能在视图模型中使用它呢?     

解决方法

        那不是有效的C#。您可以改用静态方法:
public static class ReferenceData
{
    public static IEnumerable<SelectListItem> GetStatusType()
    {
        return new[]
        {
            new SelectListItem { Value = \"0\",Text = \"Release\" },new SelectListItem { Value = \"1\",Text = \"Beta\" },new SelectListItem { Value = \"2\",Text = \"Alpha\" },};
    }
}
接着 :
public class AdminViewModel
{
    public string Status { get; set; }

    [DisplayName(\"Status\")]
    public IEnumerable<SelectListItem> Statuses
    {
        get
        {
            return ReferenceData.GetStatusType();
        }
    }
}
终于在视图中:
@Html.DropDownListFor(
    x => x.Status,new SelectList(Model.Statuses,\"Value\",\"Text\"),new { style = \"display: inline;\" }
)
    ,        您确定要让类是静态的,而不是属性吗?如果您具有静态属性,则无需创建实例即可访问属性。更多信息:静态类和静态类成员(C#编程指南):     ,        您想这样做:
    public class ReferenceData
    {
        private static readonly SelectListItem[] Items = new[]
                {
                    new SelectListItem { Value = \"0\",};

        [DisplayName(\"Status\")]
        public IEnumerable<SelectListItem> StatusType
        {
            get { return Items; }
        }
    }
(在使用类实例时将项目设为静态)。 您还可以使用单例:
    public class ReferenceData
    {
        private static readonly ReferenceData _instance = new ReferenceData();

        private ReferenceData()
        {

        }

        public ReferenceData Instance { get { return _instance; } }

        [DisplayName(\"Status\")]
        public IEnumerable<SelectListItem> StatusType
        {
            get
            {
                return new[]
                {
                    new SelectListItem { Value = \"0\",};
            }
        }
    }
并在模型中使用它:
public class adminViewModel
{
    public HouseData   HouseData { get; set; }
    public string Status { get; set; }
    public ReferenceData ReferenceData { get {return ReferenceData.Instance; } }
}
    ,        首先,像这样创建模型。
public class ReferenceData
{
    public string SelectRuleId { get; set; }

    public IEnumerable<SelectListItem> StatusType
    {
        get
        {
            return new[]
            {
                new SelectListItem { Value = \"0\",};
        }
    }
}
然后在这样的任何控制器中创建新动作。
   public ActionResult About()
    {
        var model = new ReferenceData();

        ViewBag.Message = \"DropDown Selected For \";

        return View(model);
    }
然后在您的操作视图中使用此HTML ...
@model MVCDropDownList.Models.AnswersViewModel

 @using (Html.BeginForm())  
    {  @Html.DropDownListFor(x => x.SelectRuleId,new SelectList(Model.StatusType,new { id = \"DId\" })  
    }
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...