I am trying to display a list of radio buttons for a list of values in ASP.NET MVC. I am confused on how to write the model for the radio button list and the view to be displayed. I need to display a static list of values.
我试图在ASP.NET MVC中显示值列表的单选按钮列表。我对如何为单选按钮列表和要显示的视图编写模型感到困惑。我需要显示一个静态值列表。
Can some please help.
请一些人帮忙。
Thanks.
谢谢。
1 个解决方案
#1
7
i'm implementing values on viewModel you can take it from model if you want
我正在viewModel上实现值,你可以根据需要从模型中获取它
viewModel
视图模型
public class PropMgmtViewModel
{
public Property Property { get; set; }
public IEnumerable<Property> Properties { get; set; }
public SelectList Cities { get; private set; }
static Dictionary<int, string> CitiesDict = new Dictionary<int, string>()
{
{ 1 ,"Chicago"},
{ 2 ,"New York"},
{ 3 ,"Zimbabwe"},
};
public PropMgmtViewModel()
{
Cities = new SelectList(CitiesDict, "Key", "Value");
}
view code i also included the seleclist propert to show you how it's done
查看代码我还包括seleclist属性,以向您展示它是如何完成的
@foreach (var radioitem in Model.Cities)
{
@radioitem.Text;
@Html.RadioButtonFor(model => model.Property.City, radioitem.Value);
}
@Html.DropDownListFor(model => model.Property.City, Model.Cities,"Seciniz")
#1
7
i'm implementing values on viewModel you can take it from model if you want
我正在viewModel上实现值,你可以根据需要从模型中获取它
viewModel
视图模型
public class PropMgmtViewModel
{
public Property Property { get; set; }
public IEnumerable<Property> Properties { get; set; }
public SelectList Cities { get; private set; }
static Dictionary<int, string> CitiesDict = new Dictionary<int, string>()
{
{ 1 ,"Chicago"},
{ 2 ,"New York"},
{ 3 ,"Zimbabwe"},
};
public PropMgmtViewModel()
{
Cities = new SelectList(CitiesDict, "Key", "Value");
}
view code i also included the seleclist propert to show you how it's done
查看代码我还包括seleclist属性,以向您展示它是如何完成的
@foreach (var radioitem in Model.Cities)
{
@radioitem.Text;
@Html.RadioButtonFor(model => model.Property.City, radioitem.Value);
}
@Html.DropDownListFor(model => model.Property.City, Model.Cities,"Seciniz")