如何使用Html.DropDownList中含有可为空枚举属性模型的强类型的视图? [英] How to use a Html.DropDownList in a strongly-typed view for a model containing a nullable enum property?
问题描述
我有一个模型类,如下所示:
命名空间MvcApplication1.Models
{
公共枚举性别{男,女};
公共类Person
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; } [必需(的ErrorMessage =请选择女性或男性。)
公共性?性爱{搞定;组; }
}
}
的修改
操作一样:
命名空间MvcApplication1.Controllers
{
公共类HomeController的:控制器
{
公众的ActionResult编辑()
{
变种人=新的Person {ID = 1,名称=有人,性爱= Sex.Male};
返回查看(人);
}
}
}
问题1
如何来标记修改
鉴于这样的,有含3个选项下拉控制:不限,女和男 。对于人
传递给视图,必须选择男性。
问题2
如何弥补创建
视图并保留下拉控制中选择 - 选择 - 默认情况下。
下面是创建
动作:
公众的ActionResult的Create()
{
变种人=新的Person();
返回查看(人);
}
有一个<一个href=\"http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc\">good已经回答了如何枚举转换成的SelectList 但我会重用code直列简单的答案。
公众的ActionResult编辑()
{
变种人=新的Person {ID = 1,名称=有人,性爱= Sex.Male};
清单&LT;对象&gt;值=新的List&LT;对象&gt;();
values.Add(新{ID =选择,名称= - 选择 - });
values.AddRange(从Enum.GetValues性爱(typeof运算(性别))
选择新的{ID =性别,姓名= sex.ToString()});
计算机[两性] =新的SelectList(价值观,ID,姓名,Person.Sex);
返回查看(人);
}
现在的Edit.cshtml观点:
@model Test.Models.Person@ {
ViewBag.Title =编辑;
布局=〜/查看/共享/ _Layout.cshtml
}&LT; H2&gt;编辑&LT; / H&GT;&LT;脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
&LT;脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
&LT;&字段集GT;
&LT;传奇&GT;与人LT; /传说&GT; @ Html.HiddenFor(型号=&GT; model.Id) &LT; DIV CLASS =编辑标记&GT;
@ Html.LabelFor(型号=&GT; model.Name)
&LT; / DIV&GT;
&LT; DIV CLASS =主编场&GT;
@ Html.EditorFor(型号=&GT; model.Name)
@ Html.ValidationMessageFor(型号=&GT; model.Name)
&LT; / DIV&GT; &LT; DIV CLASS =编辑标记&GT;
@ Html.LabelFor(型号=&GT; model.Sex)
&LT; / DIV&GT;
&LT; DIV CLASS =主编场&GT;
@ Html.DropDownListFor(型号=&GT; model.Sex,(的SelectList)ViewData的[两性])
@ Html.ValidationMessageFor(型号=&GT; model.Sex)
&LT; / DIV&GT; &所述p为H.;
&LT;输入类型=提交值=保存/&GT;
&所述; / P&GT;
&LT; /字段集&GT;
}&LT; DIV&GT;
@ Html.ActionLink(返回目录,索引)
&LT; / DIV&GT;
现在的控制动作的形式邮寄到:
[HttpPost]
公众的ActionResult编辑(人人)
{
VAR了newName = person.Name;
VAR newSex = person.Sex; 返回RedirectToAction(指数,家);
}
在后编辑动作线;
现在用的返回RedirectToAction(指数,家)休息调试模式运行项目。看你怎么可以在视图中更改表单值,然后你需要在发布至动作做什么?还有其他的选择不是使用的ViewData传递列表中,但他们在复杂的例子,是丰富的。
的创建
操作是这样的:
公众的ActionResult的Create()
{
人人=新的Person();
清单&LT;对象&gt;值=新的List&LT;对象&gt;();
values.Add(新{ID =选择,名称= - 选择 - });
values.AddRange(从Enum.GetValues性爱(typeof运算(性别))
选择新的{ID =性别,姓名= sex.ToString()});
计算机[两性] =新的SelectList(价值观,ID,姓名);
返回查看(人);
}
默认选择列表项将是第一个,这样它会显示 - 选择 - 作为默认选择。
I have a model class as follows:
namespace MvcApplication1.Models
{
public enum Sex { Male, Female };
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[Required(ErrorMessage="Please select either Female or Male.")]
public Sex? Sex { get; set; }
}
}
The Edit
action does :
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Edit()
{
var Person = new Person { Id = 1, Name = "Someone", Sex = Sex.Male };
return View(Person);
}
}
}
Question 1
How to mark up the Edit
view such that there is a dropdown control containing 3 options: "--Select--", "Female", and "Male". For the Person
passed to the view, "Male" must be selected.
Question 2
How to make up the Create
view and leave the dropdown control selecting "--Select--" by default.
Here is the Create
action:
public ActionResult Create()
{
var Person = new Person();
return View(Person);
}
There is a good answer already on how to convert an Enum into a SelectList but I'll reuse that code inline to simply the answer.
public ActionResult Edit()
{
var Person = new Person { Id = 1, Name = "Someone", Sex = Sex.Male };
List<object> values = new List<object>();
values.Add(new { ID = "choose", Name = "--Select--" });
values.AddRange(from Sex sex in Enum.GetValues(typeof(Sex))
select new { ID = sex, Name = sex.ToString() });
ViewData["sexes"] = new SelectList(values, "Id", "Name", Person.Sex);
return View(Person);
}
Now the Edit.cshtml view:
@model Test.Models.Person
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Sex)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Sex, (SelectList)ViewData["sexes"])
@Html.ValidationMessageFor(model => model.Sex)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Now the control action to post the form to:
[HttpPost]
public ActionResult Edit(Person person)
{
var newName = person.Name;
var newSex = person.Sex;
return RedirectToAction("index", "home");
}
Now run the project in debug mode with a break on the return RedirectToAction("index", "home");
line in the post-to Edit action. See how you can change form values in the view and then do what you need to do in the posted-to action? There are other options than using ViewData to pass the list but they complicate the example and are plentiful.
The Create
action would look like this:
public ActionResult Create()
{
Person person = new Person();
List<object> values = new List<object>();
values.Add(new { ID = "choose", Name = "--Select--" });
values.AddRange(from Sex sex in Enum.GetValues(typeof(Sex))
select new { ID = sex, Name = sex.ToString() });
ViewData["sexes"] = new SelectList(values, "Id", "Name");
return View(person);
}
The default select list item will be the first one so it would show "--Select--" as the default choice.
这篇关于如何使用Html.DropDownList中含有可为空枚举属性模型的强类型的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!