我想将user.Id传递到局部视图,以在局部视图中填充“ @ Html.EditorFor(model => model.UserID,新{@Value = id})”。在此局部视图模型中,我得到的是null。父视图的代码是
@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
部分视图的代码是
@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>
这在部分视图中引发异常,即“ System.NullReferenceException:'对象引用未设置为对象的实例。'”请指导我我错了。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);
}
public ActionResult MemberDetails(int id)
{
var user = db.users.Where(x => x.id == id).FirstOrDefault();
return View(user);
}