将 id 从父视图传递到部分视图但失败
Passing id from parent View to Partial view but failed
我想将 user.Id 传递给局部视图以在局部视图中填充 @Html.EditorFor(model => model.UserID, new { @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: 'Object reference not set to an instance of an object.'
局部视图。
哪里不对请指点一下
控制器:
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);
}
我想将 user.Id 传递给局部视图以在局部视图中填充 @Html.EditorFor(model => model.UserID, new { @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: 'Object reference not set to an instance of an object.'
局部视图。
哪里不对请指点一下
控制器:
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);
}