Asp.net mvc MVC4:DropDownList有效,但不适用于DropDownList
我想要行政方面的登记表 现在我的Asp.net mvc MVC4:DropDownList有效,但不适用于DropDownList,asp.net-mvc,asp.net-mvc-4,model-binding,html.dropdownlistfor,html-select,Asp.net Mvc,Asp.net Mvc 4,Model Binding,Html.dropdownlistfor,Html Select,我想要行政方面的登记表 现在我的DropDownList工作正常,但我不知道如何获取值并将其写入数据库(Sql server compact)。然后我又想,也许使用DropDownList会更好,而且可以工作,但我得到了错误。这是我的模型 public class UserModel { public int UserId { get; set; } [Required] [EmailAddress] [StringLe
DropDownList
工作正常,但我不知道如何获取值并将其写入数据库(Sql server compact)。然后我又想,也许使用DropDownList
会更好,而且可以工作,但我得到了错误。这是我的模型
public class UserModel
{
public int UserId { get; set; }
[Required]
[EmailAddress]
[StringLength(100)]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email ID ")]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 6)]
[Display(Name = "Password ")]
public string Password { get; set; }
[Required]
[Display(Name = "First Name ")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name ")]
public string LastName { get; set; }
[Required]
[Display(Name = "Address ")]
public string Address { get; set; }
public List<string> PossibleRights;
[Required]
[Display(Name = "Access Rights")]
public string AccessRight { get; set; }
}
我的看法
<h4>Signup Now!</h4>
<div class="editor-label">@Html.LabelFor(u=> u.FirstName)</div>
<div class="editor-field"> @Html.TextBoxFor(u=> u.FirstName)</div>
<br/>
<div class="editor-label">@Html.LabelFor(u=> u.LastName)</div>
<div class="editor-field"> @Html.TextBoxFor(u=> u.LastName)</div>
<br/>
<div class="editor-label">@Html.LabelFor(u=> u.Address)</div>
<div class="editor-field"> @Html.TextBoxFor(u=> u.Address)</div>
<br/>
<div class="editor-label">@Html.LabelFor(u=> u.Email)</div>
<div class="editor-field"> @Html.TextBoxFor(u=> u.Email)</div>
<br/>
<div class="editor-label">@Html.LabelFor(u=> u.Password)</div>
<div class="editor-field"> @Html.PasswordFor(u=> u.Password)</div>
<br/>
<div class="editor-label">@Html.LabelFor(u=> u.AccessRight)</div>
<div class="editor-field"> @Html.DropDownList("right")</div>
<br/>
<input type="submit" value="Register"/>
立即注册!
@LabelFor(u=>u.FirstName)
@Html.TextBoxFor(u=>u.FirstName)
@LabelFor(u=>u.LastName)
@Html.TextBoxFor(u=>u.LastName)
@LabelFor(u=>u.Address)
@Html.TextBoxFor(u=>u.Address)
@LabelFor(u=>u.Email)
@Html.TextBoxFor(u=>u.Email)
@LabelFor(u=>u.Password)
@Html.PasswordFor(u=>u.Password)
@LabelFor(u=>u.AccessRight)
@Html.DropDownList(“右”)
当我尝试将@Html.DropDownList(“右”)
更改为
@Html.DropDownListFor((u=>u.AccessRight,新选择列表(“右”、“值”、“文本”),“--Select right-->”)我收到错误。编辑:错误表示“HttpException未由用户代码处理”
数据绑定:“System.Char”不包含名为“value”的属性
提前谢谢。试试这个
@Html.DropDownListFor(model => model.AccessRight, new SelectList(ViewBag.right , "id", "Text "))
在模型中,访问权限应包含存在于Viewbag.right中的有效id,该id为可枚举类型
访问权限应包含要在下拉列表中显示为选中的值
编辑:-
@Html.DropDownListFor(model => model.AccessRight, new SelectList(ViewBag.right , "value", "Text "))
Datavalue字段的名称应与列表相同
希望这有助于尝试更改以下代码
从
@Html.DropDownList(“右”)
到
@Html.DropDownList(“AccessRight”,“在此处提供项目列表”)
提交时,您将自动在模型中获取AccessRight的值。我最后将其添加为“编辑”。仍然是相同的错误,现在它表示的值不包含名为“id”的属性"所以我的模型中的AccessRight不应该是字符串,应该是可枚举类型?没有得到模型中带有AccessRight的部分。你能解释更多可能是什么吗?访问权限应该包含一个你想在下拉列表中显示为选中的值。它应该是整数类型。因此它将从你的viewbag.right中检查,它是可枚举类型的,并且将其显示为预选。非常感谢我的工作,但现在我在db.SaveChanges();
注册操作的一部分上出现错误。表示DbUpdateException未经用户代码处理。有什么想法吗?或者我应该问新问题吗?我想这是因为我试图在post方法中添加用户,而不是在get方法中添加用户
@Html.DropDownListFor(model => model.AccessRight, new SelectList(ViewBag.right , "value", "Text "))
<div class="editor-field"> @Html.DropDownList("right")</div>
<div class="editor-field"> @Html.DropDownList("AccessRight","Provide list of items here")</div>