I get this error when i try to post back my bound model for update:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult EditUser(Int32, EtlGui.ViewModels.UsersEdit)' in 'EtlGui.Controllers.UsersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
Here are my Actions:
public ActionResult EditUser(int id)
{
User user = Database.Session.Load<User>(id);
if (user == null)
return HttpNotFound();
return View(new UsersEdit
{
username = user.username,
email = user.email,
Roles = Database.Session.Query<Role>().AsEnumerable().Select(role => new RoleCheckbox
{
id = role.id,
ischecked = user.Roles.Contains(role),
name = role.name
}).ToList()
});
}
[HttpPost,ValidateAntiForgeryToken]
public ActionResult EditUser(int id,UsersEdit edit )
{
var user = Database.Session.Load<User>(id);
if (user == null)
return HttpNotFound();
syncRole(edit.Roles,user.Roles);
if (Database.Session.Query<User>().Any(u => u.username == edit.username && u.id != id))
ModelState.AddModelError("username","username must be unique");
if (!ModelState.IsValid)
return View(edit);
user.username = edit.username;
user.email = edit.email;
Database.Session.Update(user);
return RedirectToAction("UsersServices");
}
and my view:
@model EtlGui.ViewModels.UsersEdit
....
<h2 class="heading">Edit User @Model.username</h2>
<form action="@Url.Action("EditUser")" method="POST" class="login">
@Html.AntiForgeryToken()
@Html.ValidationSummary()
....
<div class="col-lg-3 label">user name :</div>
<div class="col-lg-4">
<input type="text" name="username" class="form-control" value="@Model.username">
</div>
<div class="col-lg-3 label">email :</div>
<div class="col-lg-4">
<input type="text" name="email" class="form-control" value="@Model.email">
</div>
<div class="col-lg-3 label">password :</div>
<div class="col-lg-4">
<input type="password" name="password" class="form-control">
</div>
<h1 class="heading">select Roles</h1>
@Html.Partial("_RoleEditor", Model.Roles)
<button type="submit" class="btn darkblue-btn">Update User</button>
</form>
idin the form (and please take some time for format your code properly so its readable) - user3559349HtmlHelpermethods, including@using(Html.BeginForm()) { ...(but its appears from your code that your doing every thing possible to ensure model binding does not work) - user3559349