asp.net-mvc - 获取主视图提交的部分视图数据
问题描述
我有一个教师班和课程班。我正在为教师创建编辑表单,用户可以在其中编辑教师的信息,如姓名、职称和教师正在教授的课程。我创建了一个局部视图来显示教师的课程信息。我在主编辑视图中只有一个提交按钮。当我提交表单并获取教师对象时,尽管我已更新课程字段,但其课程对象为空。有人可以在这里帮助我吗?
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);
}
解决方案
我正在回答,因为评论不适合我正在写的内容。我仍然建议改变你调用局部视图的方式是你最好的选择:
@Html.ActionLink("Edit Course", "EditCourse", new { Model.CurrentTeacherID })
这显然需要您更改 TeacherTemplateController 中的操作方法(变量和参数不同,但您明白了):
public ActionResult EditCourse(int teacherID)
{
TeacherTemplate model = new TeacherTemplate(teacherID);
return PartialView(model);
}
请注意,此方法需要您创建一个EditCourse
视图。
推荐阅读
- python - Python缩进不清楚
- python - 我在使用 @login_required 装饰器时遇到错误,它在 /accounts/login/ 处显示 TemplateDoesNotExist
- openapi - 有没有办法覆盖 OAS3 中的属性描述和示例?
- oracle - Oracle 函数错误:ORA-00932:不一致的数据类型:预期的 CHAR 得到了 MDSYS.SDO.GEOMETRY
- html - 在悬停时更改单个 li 的颜色
- python - 如何读取csv文件并提取特定的coulmn?
- asp.net - 在 Azure 应用服务中发布自托管 Owin 应用
- ios - 如何在协议中设置读写内部、只读外部属性
- jquery - 使用 Django 模板标签中的数据设置目标 id 甚至目标类时,折叠 .data-target 不起作用
- markdown - 如何在 Markdown 中转义“>”字符