【问题标题】:Get partial view data on main view submit获取主视图提交的部分视图数据
【发布时间】:2019-02-27 05:18:54
【问题描述】:

我有一个教师课程和课程课程。我正在为教师创建编辑表单,用户可以在其中编辑教师的信息,如姓名、职称和教师正在教授的课程。我创建了一个局部视图来显示教师的课程信息。我在主编辑视图中只有一个提交按钮。当我提交表单并获取教师对象时,尽管我已经更新了课程字段,但它的课程对象为空。有人可以在这里帮助我吗?

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);
            }

【问题讨论】:

  • 看起来你在课程部分视图中使用的是“teacherTemplate”而不是“moduleTemplate”
  • 我修好了。请立即检查。最初,我将 Courses 对象传递给 _editCourse 部分视图,但是当我提交时,课程对象为空,所以我尝试传递教师模板,因为主视图有 TeacherTemplate 作为模型,但它仍然是相同的结果。每当我提交表单课程对象为空。
  • 我正在打电话,所以不能 100% 确定这一点,但在主编辑中,您用于将模型传递给课程编辑操作方法的 @Html.Action 是错误的。你不能传递整个模型,因为你基本上是以 Action/Controller/Model 的形式传递一个 URL,我记得这是不可能的。相反,尝试将teacherTemplateId 传递给该操作方法并从该方法中构建一个新模型,并确保设置正确的MapRoute
  • 我也试过这个:@Html.Partial("_editCourse", Model) 仍然没有帮助。
  • EditCourse 方法的参数没有类型,是拼写错误吗?

标签: asp.net-mvc partial-views


【解决方案1】:

我之所以回答是因为 cmets 不适合我所写的内容。我仍然建议改变你调用局部视图的方式是你最好的选择:

@Html.ActionLink("Edit Course", "EditCourse", new { Model.CurrentTeacherID })

这显然需要您更改 TeacherTemplateController 中的操作方法(变量和参数不同,但您明白了):

public ActionResult EditCourse(int teacherID)
{
    TeacherTemplate model = new TeacherTemplate(teacherID);
    return PartialView(model);
}

请注意,此方法需要您创建一个EditCourse 视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多