我是MVC的新手
我正在使用实体框架。
我有三个数据库表
User: UserID,UserName
Role: RoleID,RoleName
UserRoles: ID,UserID,RoleID
UserDataModel.cs
[MetadataTypeAttribute(typeof(UserMetaData))]
public partial class User
{
[Required(ErrorMessage = "Please enter your again")]
[DataType(DataType.Password)]
[Compare("Password")]
public String ConfirmPassword { get; set; }
}
public class UserMetaData
{
[Required(AllowEmptyStrings = false, ErrorMessage = "You forgot to enter your name.")]
[Display(Name = "Your Name")]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Your name is too long. Please change your name if you can !")]
public String UserName { get; set; }
[Required(ErrorMessage = "Do you know your gender ?? if you don't know, leave it !!")]
public Boolean Gender { get; set; }
[Required(ErrorMessage = "You forgot to select your current country.")]
public Int32 CountryID { get; set; }
[Required(ErrorMessage = "Please tell us your 'Happy birth date'. We may brings you a huge gift for you on your birthday.")]
[RegularExpression("^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((19|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$", ErrorMessage = "Oh Noo!! Please enter date in dd/MM/yyy formate")]
public DateTime DOB { get; set; }
[Required(ErrorMessage = "Please tell us your email address. we assure you that we can't reach you with your email address.")]
[RegularExpression("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", ErrorMessage = "Oh Noo!! Please enter date in dd/MM/yyy formate")]
public String Email { get; set; }
[Required(ErrorMessage = "Please tell us your contact number. we assure that we wont give this number to anyone.")]
public String ContactNo { get; set; }
//[Required(ErrorMessage = "You have to select atleaset one role for you.")]
//public List<Roles> Roles { get; set; }
[Required(ErrorMessage = "Your security can not be compromised. Please enter your password")]
[DataType(DataType.Password)]
public String Password { get; set; }
}
RegistrationController.cs
DbMVCEntities db = new DbMVCEntities();
public ActionResult RegisterUser()
{
return View();
}
[HttpPost]
public ActionResult RegisterUser(User _user, string[] Roles)
{
try
{
db.Users.AddObject(_user);
db.SaveChanges();
for (int i = 0; i <= Roles.Length; i++)
{
UserRole _UserRole = new UserRole();
_UserRole.UserID = _user.UserID;
_UserRole.RoleID = Convert.ToInt32(Roles[0]);
db.UserRoles.AddObject(_UserRole);
db.SaveChanges();
}
}
catch
{
}
return RedirectToAction("RegisterUser");
}
public ActionResult Edit(int id)
{
try
{
var User = db.Users.SingleOrDefault(x => x.UserID == id);
if (User != null)
return View("RegisterUser", User);
else
return RedirectToAction("RegisterUser");
}
catch (Exception ex)
{
throw;
}
}
RegisterUser.cshtml
@model MVCRegistration.User
@{
ViewBag.Title = "RegisterUser";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
<table width="100%">
<tr>
<td width="15%">
Name :
</td>
<td width="10%">
@Html.TextBoxFor(x => x.UserName)
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.UserName)
</td>
</tr>
<tr>
<td>
Your Name :
</td>
<td>
@Html.RadioButtonFor(x => x.Gender, true)Male
@Html.RadioButtonFor(x => x.Gender, false)Female
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.Gender)
</td>
</tr>
<tr>
<td>
Country :
</td>
<td>
@Html.DropDownListFor(x => x.CountryID, new SelectList((new MVCRegistration.DbMVCEntities()).Countries.ToList(), "CountryID", "CountryName"), "--Select--")
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.CountryID)
</td>
</tr>
<tr>
<td>
Birth Date :
</td>
<td>
@Html.TextBoxFor(x => x.DOB)
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.DOB)
</td>
</tr>
<tr>
<td>
Email :
</td>
<td>
@Html.TextBoxFor(x => x.Email)
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.Email)
</td>
</tr>
<tr>
<td>
Contact No. :
</td>
<td>
@Html.TextBoxFor(x => x.ContactNo)
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.ContactNo)
</td>
</tr>
<tr>
<td width="10%" valign="top">
Roles :
</td>
<td style="text-transform: lowercase;">
@foreach (MVCRegistration.Role role in new MVCRegistration.DbMVCEntities().Roles)
{
<input type="checkbox" name="Roles" value="@role.RoleID" /> @role.RoleName <br />
}
</td>
<td width="80%">
</td>
</tr>
<tr>
<td>
Password :
</td>
<td>
@Html.PasswordFor(x => x.Password)
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.Password)
</td>
</tr>
<tr>
<td>
System Password :
</td>
<td>
@Html.PasswordFor(x => x.ConfirmPassword)
</td>
<td width="80%">
@Html.ValidationMessageFor(x => x.ConfirmPassword)
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="Register Me" />
</td>
</tr>
</table>
<table>
<tr>
<th>
Name
</th>
<th>
Gender
</th>
<th>
Birth Date
</th>
<th>
Country
</th>
<th>
Email
</th>
<th>
Contact No
</th>
<th>
</th>
</tr>
@foreach (MVCRegistration.User _user in new MVCRegistration.DbMVCEntities().Users)
{
<tr>
<td>
@_user.UserName
</td>
<td>
@if (@_user.Gender)
{
@Html.Raw("Male");
}
else
{
@Html.Raw("Female");
}
</td>
<td>
@_user.DOB.ToString("dd/MM/yyyy")
</td>
<td>
@_user.Country.CountryName
</td>
<td>
@_user.Email
</td>
<td>
@_user.ContactNo
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = _user.UserID }) |
@Html.ActionLink("Details", "Details", new { id = _user.UserID }) |
@Html.ActionLink("Delete", "Delete", new { id = _user.UserID })
</td>
</tr>
}
</table>
}
它的工作正常!!!当我点击编辑除用户角色之外填满的所有数据复选框。
实际上我在“UserRoles”中有角色列表。
如何在编辑模式下获取用户角色列表???????
请帮帮我。如果我做错了什么,请告诉我。
答案 0 :(得分:0)
为什么要使用
@foreach (MVCRegistration.Role role in new MVCRegistration.DbMVCEntities().Roles)
{
<input type="checkbox" name="Roles" value="@role.RoleID" /> @role.RoleName <br />
}
您可以使用
@for (var i = 0; i < MVCRegistration.DbMVCEntities().Roles; i++)
{
@Html.CheckBoxFor(x => x.DbMVCEntities().Roles[i], new { @checked = "checked" })
@Html.LabelFor(x => x.DbMVCEntities().Roles[i])
}
将用户类更改为
[MetadataTypeAttribute(typeof(UserMetaData))]
public partial class User
{
[Required(ErrorMessage = "Please enter your again")]
[DataType(DataType.Password)]
[Compare("Password")]
public String ConfirmPassword { get; set; }
public IEnumerable<Role> AllRoles { get; set; }
public UserRoleModel()
{
this.AllRoles = DbMVCEntities().Roles.Select(r => new Role
{
Name = r
});
}
}
然后你可以在视图中使用这个AllRoles
答案 1 :(得分:0)
您的表单提交是否有效?public ActionResult RegisterUser(User _user, string[] Roles)
是否会使用您期望的数据进行调用?
通常,您的模型将是您提交的内容。所以,我会制作MVCRegistration.DbMVCEntities()。角色列出模型的成员。例如Roles
。您可以指定[NotMapped]属性,以便EF不会认为它已映射到数据库。然后你会做类似的事情:
@for(int i = 0; i< Model.Roles.Count; i++)
{
@Html.CheckBoxFor( x => x.Roles[i] );
}