【发布时间】:2016-11-03 12:13:34
【问题描述】:
我有一个包含学生列表的页面。呈现列表的局部视图称为“StudentManager”。我在模态中用于创建新学生的局部视图称为“NewStudent”。
这个控制器代码有几个问题。出于某种原因,在我在“NewStudent”部分视图上按下提交后,每次刷新页面时都会有一个新学生在那里,而我没有进入并按下提交......这是一个问题。
另外,我在堆栈上搜索了类似的主题,我似乎无法理解为什么 return PartialView("StudentManager",db.Students.ToList());不会自动刷新我的“StudentManager”视图。 这段代码应该在一个局部视图中给我一个列表,另一个局部视图应该让我创建一个新的列表项,然后告诉列表局部视图进行更新。
控制器:
public ViewResult Index()
{
return View();
}
public ActionResult StudentManager()
{
return PartialView(db.Students.ToList());
}
public ActionResult NewStudent()
{
return PartialView();
}
//
// POST:
[HttpPost]
public ActionResult NewStudent(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
return PartialView();
}
Index.cshtml:
@Html.Action("StudentManager", "StudentController")
<div class="modal modal-wide fade" id="myModal4" role="dialog">
<div class="modal-dialog">
@Html.Action("NewStudent", "StudentController")
</div>
</div>
这是“NewStudent.cshtml”视图:
@model GunneryTracker.Models.Student
<fieldset>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<center>
<h4 class="modal-title">New Student</h4>
</center>
</div>
<div class="modal-body">
<div class="col-md-12">
<div class="clearfix"></div>
<div class="x_content">
<br />
<form class="form-horizontal form-label-left" >
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12">
<label class="control-label col-md-2 col-sm-2 col-xs-12" style="margin-right:10px">Course</label>
<div class="col-md-3 col-sm-3 col-xs-12">
@Html.DropDownList("CourseID", null, "-- Select Course --", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CourseID, "", new { @class = "text-danger" })
</div>
<label class="control-label col-md-2 col-sm-2 col-xs-12" style="margin-right:10px">Location</label>
<div class="col-md-3 col-sm-3 col-xs-12">
@Html.DropDownList("LocationID", null, "-- Select Location--", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LocationID, "", new { @class = "text-danger" })
</div>
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FName)
@Html.ValidationMessageFor(model => model.FName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LName)
@Html.ValidationMessageFor(model => model.LName)
</div>
</div>
<br />
<br />
<div class="form-group">
<center>
<p>
<input type="submit" value="Create" />
</p>
</center>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Save</button>
</div>
</div>
</fieldset>
请原谅懒散的 html... 美化还没有完成...
【问题讨论】:
标签: asp.net asp.net-mvc