I am having this error how to resolve it...
Code is provided....
Controller.......
public ActionResult Edit(int id)
{
UserDTO user = new UserDTO();
user.UserID = id;
SetViewBagData(id);
return View(user);
}
[HttpPost]
public ActionResult Edit(UpdateUser model, int id)
{
try
{
UserDTO user = new UserDTO();
user.UserName = model.UserName;
new UserHandler().Update(user, id);
return RedirectToAction("Details", new RouteValueDictionary (new { id = user.UserID}));
}
catch
{
return View();
}
}
public void SetViewBagData(int _userId)
{
UserDTO r = new UserDTO();
ViewBag.UserId = _userId;
ViewBag.List_boolNullYesNo = this.List_boolNullYesNo();
ViewBag.RoleId = new SelectList(r.Role.OrderBy(p => p.RoleName), "RoleID", "RoleName");
}
public List<SelectListItem> List_boolNullYesNo()
{
var _retVal = new List<SelectListItem>();
try
{
_retVal.Add(new SelectListItem { Text = "Not Set", Value = null });
_retVal.Add(new SelectListItem { Text = "Yes", Value = bool.TrueString });
_retVal.Add(new SelectListItem { Text = "No", Value = bool.FalseString });
}
catch { }
return _retVal;
}
public PartialViewResult AddUserRoleReturnPartialView(int id, int userId)
{
new UserHandler().AddUserRole(id, userId);
SetViewBagData(userId);
return PartialView("_ListUserRoleTable", userId);
}
Business Logic....
public class UserHandler
{
PriviligeContext db = new PriviligeContext();
public List<UserDTO> GetData()
{
var query = from u in db.Users
select new UserDTO
{
UserName = u.Username
};
return query.ToList();
}
public void Insert(UserDTO user)
{
User u = new User();
u.Username = user.UserName;
db.Users.Add(u);
db.SaveChanges();
}
public void Update(UserDTO user, int id)
{
var u = db.Users.FirstOrDefault(i => i.UserID == id);
u.Username = user.UserName;
db.SaveChanges();
}
public void AddUserRole(int id, int userId)
{
Role role = db.Roles.Find(id);
User user = db.Users.Find(userId);
if (!role.Users.Contains(user))
{
role.Users.Add(user);
db.SaveChanges();
}
}
}
DTO...
namespace DTO
{
public class UserDTO
{
public int UserID { get; set; }
public string UserName { get; set; }
public virtual ICollection<RoleDTO> Role { get; set; }
}
}
namespace DTO
{
public class RoleDTO
{
public int RoleID { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public virtual ICollection<PriviligeDTO> Privilige { get; set; }
public virtual ICollection<UserDTO> User { get; set; }
}
}
Edit View......
<pre>model SampleWeb.Models.UpdateUser
@using (Html.BeginForm("Edit", "User", FormMethod.Post))
{
@Html.ValidationSummary(true)
<input name='User_Id' id='User_Id' type="hidden" value="@ViewBag.UserId" />
<fieldset>
<legend>
<h3>
User Details
</h3>
</legend>
@Html.Partial("_userDetailsEdit", Model)
<br />
<input type="submit" value="Save" />
</fieldset>
<fieldset>
<legend>
<h3>
Roles Associated with this User
</h3>
</legend>
<div class="panel" id="RolesTable">
@Html.Partial("_ListUserRoleTable", Model)
</div>
@Html.Partial("_ListEditableUserRoleTable")
<br />
<div>
@Html.ActionLink("Go to Roles", "RoleIndex", "Role")
</div>
</fieldset>
}
<br />
<div>
@Html.ActionLink("Go to Users", "Index", "Admin")
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}</pre>