Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having this error how to resolve it...
Code is provided....
Controller.......
// GET: User/Edit/5
       public ActionResult Edit(int id)
       {
           UserDTO user = new UserDTO();
           user.UserID = id;
           SetViewBagData(id);
           return View(user);

       }

       // POST: User/Edit/5
       [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();
           }
       }

XML
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;
        }

C#
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...
C#
namespace DTO
{
    public class UserDTO
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public virtual ICollection<RoleDTO> Role { get; set; }
    }
}

C#
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)
    &lt;input name='User_Id' id='User_Id' type="hidden" value="@ViewBag.UserId" /&gt;
    &lt;fieldset&gt;
        &lt;legend&gt;
            &lt;h3&gt;
                User Details
            &lt;/h3&gt;
        &lt;/legend&gt;
        @Html.Partial("_userDetailsEdit", Model)
        &lt;br /&gt;
        &lt;input type="submit" value="Save" /&gt;
    &lt;/fieldset&gt;

    &lt;fieldset&gt;
        &lt;legend&gt;
            &lt;h3&gt;
                Roles Associated with this User
            &lt;/h3&gt;
        &lt;/legend&gt;
        &lt;div class="panel" id="RolesTable"&gt;
            @Html.Partial("_ListUserRoleTable", Model)
        &lt;/div&gt;
        @Html.Partial("_ListEditableUserRoleTable")
        &lt;br /&gt;
        &lt;div&gt;
            @Html.ActionLink("Go to Roles", "RoleIndex", "Role")
        &lt;/div&gt;
    &lt;/fieldset&gt;
}
&lt;br /&gt;
&lt;div&gt;
    @Html.ActionLink("Go to Users", "Index", "Admin")
&lt;/div&gt;
&lt;div&gt;
    @Html.ActionLink("Back to List", "Index")
&lt;/div&gt;

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}</pre>
Posted
Updated 23-Jul-19 21:32pm
Comments
Krunal Rohit 3-Nov-15 2:43am    
This error is usually related to the EF. Try using debugger and narrow down your issue and the post the relevant code and error details.
Posting this much code is not going to work anyone from us.

-KR
Debug and find out the exact line.
Suvendu Shekhar Giri 3-Nov-15 2:50am    
Have you debugged it? If yes, what was your finding?
Sinisa Hajnal 3-Nov-15 2:56am    
Set a breakpoint and debug. Find exactly where it fails (and doing that you'll probably resolve it yourself). Good luck
John C Rayan 3-Nov-15 6:06am    
Your problem is this line of code.
[HttpPost]
public ActionResult Edit(UpdateUser model, int id)

You are expecting route parameter id but you are passing it as user_id. Change it to user_id.

1 solution

I have sent back to you the amended project in working condition
 
Share this answer
 
Comments
Member 3777988 22-Dec-16 17:15pm    
please, can you add the comment with the solution, i have the same problem

[HttpPost]
public ActionResult Update(DataEventViewModel model)
{
model.Update();
return RedirectToAction("Detail", "DataActivityProject", new { dataActivityProjectID = model._DataEvent.DataActivityProjectID });
}

@using (Html.BeginForm("Update", "DataEvent", null, FormMethod.Post, null))
{


@Html.Partial("_DataEventForm")





@Html.ActionLink("Cancel", "Detail", "DataActivityProject", new { dataActivityProjectID = Model._DataEvent.DataActivityProject.DataActivityProjectID }, new { @class = "btn btn-warning" })
@*Delete*@



}
John C Rayan 30-Dec-16 9:18am    
Not necessarily same issue. Can you please post what error you are getting? If possible create a new question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900