如何在模型属性上设置多个选项
How can I set multiple options on a model attribute
我正在尝试详细说明一个模型(例如用户),它可以 select 从多个选项(列表)中选择一种颜色
如何将其写入模型并在视图中公开?
我只需要为颜色编写一个单独的模型,或者这种方法是否正确:
//user.cs
public class User
{
public string Name { get; set; }
public Colors Colors { get; set; }
}
//colors.cs
public class Colors
{
public User User { get; set; }
public enum Colors ???
}
你快到了。如果您只想提供 Color
枚举,则 Color
class 不是必需的。考虑到这一点,试试这个:
public enum Color
{
Red,
Green,
Blue
}
public class User
{
public String Name { get; set; }
public Color Color { get; set; }
}
public class UserColorAddEditViewModel
{
public String Name { get; set; }
public Color Color { get; set; }
public IList<SelectListItem> ColorSelectList { get; set; }
}
然后在View中,可以使用UserColorAddEditViewModel
模型,使用下面的HtmlHelper
:
@Html.DropDownListFor(model => model.Color, Model.ColorSelectList)
或如Icemanind
所述,您可以简单地使用:
@Html.EnumDropDownListFor(model => model.Color)
并且您废弃了 ColorSelectList
变量(这意味着您也可以忽略下面的部分)。
如果您决定使用 DropDownListFor
帮助程序,则需要在定义的 Controller
class 中启动此变量,如下所示。
public class MyController : Controller
{
public ActionResult Index()
{
UserColorAddEditViewModel Model = new UserColorAddEditViewModel();
Model.ColourSelectList = Enum.
GetValues(typeof(Color)).
Cast<Color>().
Select(c => new SelectListItem { Text = c.ToString(), Value = ((int)c).ToString()})
.ToList();
return View(Model);
}
}
您的基本用户 class 将如下所示。我将你的名字 属性 分解为名字和姓氏。
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
我稍微更改了您的颜色 class 以防万一(有一天)您希望能够使用数据库路由而不是使用一组固定的颜色。以单数形式而非复数形式命名您的 class,例如 Color 而不是 Colours。
public class Colour
{
public int Id { get; set; }
public string Name { get; set; }
}
用户视图模型将传递给视图。此视图模型将包含用户信息和将在下拉列表中使用的颜色列表。
public class UserViewModel
{
public string Name { get; set; }
public int ColourId { get; set; }
public List<Colour> Colours { get; set; }
}
在控制器的操作方法中,您创建了上面视图模型的一个实例,并用用户数据和所有颜色的列表填充它。
public ActionResult Edit(int userId)
{
// Get your user's information
User user = userRepository.GetById(userId);
UserViewModel model = new UserViewModel();
model.Name = user.FirstName + " " + user.LastName;
// This can be separated into a repository class of its own if you need to use colours in another action method
model.Colours = new List<Colour>
{
new Colour { Id = 1, Name = "Blue" },
new Colour { Id = 2, Name = "Green" },
new Colour { Id = 3, Name = "Red" },
new Colour { Id = 4, Name = "Yellow" }
};
return View(model);
}
然后在您的视图中,您将看到类似的内容:
<h1>@Model.Name</h1>
<p>Please select a colour:</p>
@Html.DropDownListFor(
x => x.ColourId,
new SelectList(Model.Colours, "Id", "Name", Model.ColourId),
"-- Select --"
)
尝试一下代码,看看它是如何为您工作的。
我正在尝试详细说明一个模型(例如用户),它可以 select 从多个选项(列表)中选择一种颜色
如何将其写入模型并在视图中公开? 我只需要为颜色编写一个单独的模型,或者这种方法是否正确:
//user.cs
public class User
{
public string Name { get; set; }
public Colors Colors { get; set; }
}
//colors.cs
public class Colors
{
public User User { get; set; }
public enum Colors ???
}
你快到了。如果您只想提供 Color
枚举,则 Color
class 不是必需的。考虑到这一点,试试这个:
public enum Color
{
Red,
Green,
Blue
}
public class User
{
public String Name { get; set; }
public Color Color { get; set; }
}
public class UserColorAddEditViewModel
{
public String Name { get; set; }
public Color Color { get; set; }
public IList<SelectListItem> ColorSelectList { get; set; }
}
然后在View中,可以使用UserColorAddEditViewModel
模型,使用下面的HtmlHelper
:
@Html.DropDownListFor(model => model.Color, Model.ColorSelectList)
或如Icemanind
所述,您可以简单地使用:
@Html.EnumDropDownListFor(model => model.Color)
并且您废弃了 ColorSelectList
变量(这意味着您也可以忽略下面的部分)。
如果您决定使用 DropDownListFor
帮助程序,则需要在定义的 Controller
class 中启动此变量,如下所示。
public class MyController : Controller
{
public ActionResult Index()
{
UserColorAddEditViewModel Model = new UserColorAddEditViewModel();
Model.ColourSelectList = Enum.
GetValues(typeof(Color)).
Cast<Color>().
Select(c => new SelectListItem { Text = c.ToString(), Value = ((int)c).ToString()})
.ToList();
return View(Model);
}
}
您的基本用户 class 将如下所示。我将你的名字 属性 分解为名字和姓氏。
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
我稍微更改了您的颜色 class 以防万一(有一天)您希望能够使用数据库路由而不是使用一组固定的颜色。以单数形式而非复数形式命名您的 class,例如 Color 而不是 Colours。
public class Colour
{
public int Id { get; set; }
public string Name { get; set; }
}
用户视图模型将传递给视图。此视图模型将包含用户信息和将在下拉列表中使用的颜色列表。
public class UserViewModel
{
public string Name { get; set; }
public int ColourId { get; set; }
public List<Colour> Colours { get; set; }
}
在控制器的操作方法中,您创建了上面视图模型的一个实例,并用用户数据和所有颜色的列表填充它。
public ActionResult Edit(int userId)
{
// Get your user's information
User user = userRepository.GetById(userId);
UserViewModel model = new UserViewModel();
model.Name = user.FirstName + " " + user.LastName;
// This can be separated into a repository class of its own if you need to use colours in another action method
model.Colours = new List<Colour>
{
new Colour { Id = 1, Name = "Blue" },
new Colour { Id = 2, Name = "Green" },
new Colour { Id = 3, Name = "Red" },
new Colour { Id = 4, Name = "Yellow" }
};
return View(model);
}
然后在您的视图中,您将看到类似的内容:
<h1>@Model.Name</h1>
<p>Please select a colour:</p>
@Html.DropDownListFor(
x => x.ColourId,
new SelectList(Model.Colours, "Id", "Name", Model.ColourId),
"-- Select --"
)
尝试一下代码,看看它是如何为您工作的。