通过viewmodel将引用/静态数据传递给MVC3中的视图

时间:2011-06-13 08:02:43

标签: c# asp.net-mvc

我创建了一个类来保存一些静态数据:

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

我是否真的需要使它成为一个非静态的实例化类,然后才能在viewmodel中使用它,即使数据总是静态的?

4 个答案:

答案 0 :(得分:0)

你确定你希望你的班级是静态的而不是财产吗?如果您具有静态属性,则无需创建实例即可访问属性。更多信息:Static Classes and Static Class Members (C# Programming Guide)

答案 1 :(得分:0)

你想这样做:

    public class ReferenceData
    {
        private static readonly SelectListItem[] Items = new[]
                {
                    new SelectListItem { Value = "0", Text = "Release" },
                    new SelectListItem { Value = "1", Text = "Beta" },
                    new SelectListItem { Value = "2", Text = "Alpha" },
                };

        [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", 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 {return ReferenceData.Instance; } }
}

答案 2 :(得分:0)

那是无效的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;" }
)

答案 3 :(得分:0)

首先创建这样的模型..

public class ReferenceData
{
    public string SelectRuleId { get; set; }

    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 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, "Value", "Text"), new { id = "DId" })  
    }