我的代码类似于:
class StudentsViewModel:
public class StudentsViewModel
{
public List<Student> Students { get; set; }
public Student SelectedStudent { get; set; }
public string DisplayMode { get; set; }
}
StudentsController:
[HttpPost]
public ActionResult New(int? page, int? SelectedGroup)
{
// some code
StudentsViewModel model = new StudentsViewModel();
model.Students = db.Students.ToList().ToPagedList(pageNumber, pageSize);
model.SelectedStudent = null;
model.DisplayMode = "WriteOnly";
ViewBag.IDGroup = new SelectList(db.Groups, "IDGroup", "Name");
return View("Index", model);
}
查看:索引
<form method="post">
<input type="submit" value="Add Student" formaction="/Students/new" class="btn btn-default" />
//some code
@{
if (Model.SelectedStudent != null)
{
if (Model.DisplayMode == "ReadWrite")
{
Html.RenderPartial("_EditStudent", Model.SelectedStudent);
}
}
if (Model.DisplayMode == "WriteOnly")
{
Html.RenderPartial("_InsertStudent", new StudentList.Models.Student());
}
}</form>
部分视图: _InsertStudent.cshtml
<div class="form-group">
@Html.DropDownList("IDGroup", String.Empty)
@Html.ValidationMessageFor(model => model.IDGroup)
</div>
我有一个非常大的问题,因为我的DropDownList不起作用...现在我点击按钮时显示_InsertStudent但它不起作用...如果我有
Html.RenderPartial("_InsertStudent", new StudentList.Models.Student());
直接(没有按钮)它有效......