我想有管理员方面的注册表单。
现在我的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’的属性
提前致谢。