我有一个教师班和一个课程班。我正在为老师创建编辑表单,用户可以在其中编辑老师的信息,例如老师正在教的姓名,职称和课程。我创建了局部视图以显示老师的课程信息。我在主编辑视图中只有一个提交按钮。当我提交表单并获取教师对象时,尽管我已更新课程字段,但其课程对象为null。可以请人帮我吗?
public class TeacherTemplate
{
public int teacherId { get; set;}
public string TeacherName { get; set; }
public string TeacherCourses { get; set; }
public CourseTemplate Courses
{
get => TeacherCourses == null ? null : JsonConvert.DeserializeObject<CourseTemplate>(TeacherCourses);
set => TeacherCourses = JsonConvert.SerializeObject(Courses);
}
}
public class CourseTemplate
{
public string CourseName {get; set; }
public int courseId { get; set; }
public bool Active { get; set; }
public Subjects Subject { get; set;}
}
public class Subjects
{
public string SubjectName { get; set; }
}
教师的主要编辑视图
@model TeacherTemplate
@{
ViewBag.Title = "EditTeacher";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TeacherName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ModuleTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ModuleTitle, "", new { @class = "text-danger" })
</div>
</div>
@Html.Partial("_editCourse", Model)
@*@Html.Action("EditCourse", "TeacherTemplate", new { teacherTemplate = Model })*@
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
课程部分视图
@model TeacherTemplate
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Courses.CourseName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Courses.CourseName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Courses.CourseName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Courses.Active, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Courses.Active, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Courses.Active, "", new { @class = "text-danger" })
</div>
</div>
<button type="button" id="btnAddSubjects">Add Subject</button>
if (Model.Courses.Subject.Count() > 0)
{
<div id="subjectContainer" class="form-group">
@Html.LabelFor(model => model.Courses.Subject, htmlAttributes: new { @class = "control-label col-md-2" })
@for (var i = 0; i < Model.Courses.Subject.Count(); i++)
{
<div class="col-md-10">
@Html.TextAreaFor(m => m.Courses.Subject[i].SubjectName, new { htmlAttributes = new { @class = "form-control", @cols = 80, @rows = 50 } })
</div>
}
</div>
}
</div>
}
TeacherTemplateController-在这里,当从主视图上的“提交”按钮上调用EditTeacher方法时,teacherTemplate对象具有正确绑定的所有教师信息,但是TeacherCourses属性和Courses属性/对象均为空。
public ActionResult Index()
{
return View();
}
public ActionResult EditTeacher(Guid teacherTemplateId)
{
var teacherTemplate= _teacherTemplateService.GetTeacherInfo(teacherTemplateId);
return View(teacherTemplate);
}
[HttpPost]
public ActionResult EditTeacher(TeacherTemplate teacherTemplate)
{
if (ModelState.IsValid)
{
var teacherTemplate1 = _teacherTemplateService.UpdateTeacherInfo(teacherTemplate);
return RedirectToAction("EditTeacher", new { teacherTemplateId = teacherTemplate.teacherTemplateId });
}
public ActionResult EditCourse(TeacherTemplate teacherTemplate)
{
return PartialView("_editCourse", teacherTemplate);
}
答案 0 :(得分:0)
我之所以回答,是因为评论不适合我所写的内容。我仍然建议更改部分视图的调用方式是您的最佳选择:
@Html.ActionLink("Edit Course", "EditCourse", new { Model.CurrentTeacherID })
这显然需要您在TeacherTemplateController中更改操作方法(不是相同的变量和参数,但是您有主意):
public ActionResult EditCourse(int teacherID)
{
TeacherTemplate model = new TeacherTemplate(teacherID);
return PartialView(model);
}
请注意,此方法要求您创建一个EditCourse
视图。