I want to pass user.Id to partial view to to populate "@Html.EditorFor(model => model.UserID, new { @Value = id })" in a Partial view. I am getting null in this Partial view Model. Code for parent view is
@model KhanewalNews.Models.user
@{
int id = Model.id;
}
@Html.Partial("AssignRoles",null, new ViewDataDictionary { { "id", id } });
@{
ViewBag.Title = "MemberDetails";
Layout = "~/Views/Shared/_DashboardLayout.cshtml";
}
//parent view implementation here
And code for Partial view is
@model KhanewalNews.Models.Role
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@{
int id = (int)this.ViewData["id"];
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.UserID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserID, new { @Value = id })
@Html.ValidationMessageFor(model => model.UserID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.role1)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.role1, new SelectList(
new List<Object>{
new { value = "add" , text = "add" },
new { value = "update" , text = "update" },
new { value = "delete" , text = "update"},
new { value = "super" , text = "super"}
},
"value",
"text"
))
@Html.ValidationMessageFor(model => model.role1)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
this Throws the exception in a partial view that "System.NullReferenceException: 'Object reference not set to an instance of an object.'" Please guide me where i am wrong. Controller
public ActionResult AssignRoles()
{
return View();
}
[HttpPost]
public ActionResult AssignRoles(Role r)
{
//implementation here
return View();
}
public ActionResult MemberDetails(int id)
{
var userdetail = new user();
var user = db.users.Where(x => x.id == id).FirstOrDefault();
userdetail.username = user.username;
return View(userdetail);
}