Passing reference/static data through a viewmodel to a view in MVC3
I created a class to hold some static data:
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" },
};
}
}
}
Now I would like to use that class data to populate a drop down list in a view. I have the following view model:
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;" }
)
The problem for me is that I am not sure if this is a valid way to do it because I am using a static class and when I have the get; there inside of the {} it complains during compile with an error message saying:开发者_如何转开发
static types cannot be used as return types
Do I really need to make it a non-static and instantiate the class before I can use it in the viewmodel even though the data is always static?
That's not valid C#. You could use a static method instead:
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" },
};
}
}
and then :
public class AdminViewModel
{
public string Status { get; set; }
[DisplayName("Status")]
public IEnumerable<SelectListItem> Statuses
{
get
{
return ReferenceData.GetStatusType();
}
}
}
finally in the view:
@Html.DropDownListFor(
x => x.Status,
new SelectList(Model.Statuses, "Value", "Text"),
new { style = "display: inline;" }
)
Are you sure you want your class to be static and not the property? If you have a static property, you don't have to create an instance in order to access a property. More info: Static Classes and Static Class Members (C# Programming Guide):
You want to do this:
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; }
}
}
(make the items static while using an class instance).
You can also use a singleton:
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" },
};
}
}
}
And to use it in your model:
public class adminViewModel
{
public HouseData HouseData { get; set; }
public string Status { get; set; }
public ReferenceData ReferenceData { get {return ReferenceData.Instance; } }
}
First Of all Create your Model like this..
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" },
};
}
}
}
Then Create new action in any controller like this..
public ActionResult About()
{
var model = new ReferenceData();
ViewBag.Message = "DropDown Selected For ";
return View(model);
}
Then Use this Html in your action view...
@model MVCDropDownList.Models.AnswersViewModel
@using (Html.BeginForm())
{ @Html.DropDownListFor(x => x.SelectRuleId , new SelectList(Model.StatusType, "Value", "Text"), new { id = "DId" })
}
精彩评论