我想为管理员提供注册表单。
现在我的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; }
}
我需要从dropdownlist
获取值并将其设置为Accessright
我的控制器
public List<SelectListItem> UserAccessRight()
{
var rights = new List<SelectListItem>
{
new SelectListItem {Text = "Full", Value = "0"},
new SelectListItem {Text = "Partial", Value = "1"}
};
return rights;
}
[HttpGet]
public ActionResult Register()
{
var rights = UserAccessRight();
ViewBag.right = rights;
return View();
}
[HttpPost]
public ActionResult Register(Models.UserModel user)
{
if (ModelState.IsValid)
{
using (var db = new DBaseEntities())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrpPass = crypto.Compute(user.Password);
var sysUser = db.SystemUsers.Create();
sysUser.FirstName = user.FirstName;
sysUser.LastName = user.LastName;
sysUser.Address = user.Address;
sysUser.Email = user.Email;
sysUser.Password = encrpPass;
sysUser.PasswordSalt = crypto.Salt;
sysUser.AccessRight = user.AccessRight;
db.SystemUsers.Add(sysUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("","Login data is incorrect.");
}
return View();
}
private bool IsValid(string email, string password)
{
var crypto = new SimpleCrypto.PBKDF2();
bool isValid = false;
using (var db = new DBaseEntities())
{
var user = db.SystemUsers.FirstOrDefault(u => u.Email == email);
if (user!= null)
{
if (user.Password == crypto.Compute(password, user.PasswordSalt) )
{
isValid = true;
}
}
}
return isValid;
}
我的观点
<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"/>
当我尝试将@Html.DropDownList("right")
更改为
@Html.DropDownListFor((u=>u.AccessRight, new SelectList("right", "value", "text"),"--Select Right--")
我收到错误。编辑:错误说“HttpException未被用户代码处理
数据绑定:'System.Char'不包含名为'value'的属性
提前致谢。
答案 0 :(得分:1)
试试这个
@Html.DropDownListFor(model => model.AccessRight, new SelectList(ViewBag.right , "id", "Text "))
在模型中,访问权限应包含存在于 Viewbag.right 中的有效ID,该ID为Enumerable类型。
访问权限应包含您要在下拉列表中显示的值
编辑: -
@Html.DropDownListFor(model => model.AccessRight, new SelectList(ViewBag.right , "value", "Text "))
Datavalue字段的名称应与列表
相同希望这有帮助
答案 1 :(得分:0)
尝试更改以下代码
来自
<div class="editor-field"> @Html.DropDownList("right")</div>
到
<div class="editor-field"> @Html.DropDownList("AccessRight","Provide list of items here")</div>
在提交时,您将自动获得AccessRight模型的价值。