0

我创建了一个回调 jQuery 函数来将一些集合填充到我的 ViewModel 中。当我再次调用此函数时,ViewModel 会丢失第一个回调中填充的内容。

// ----------------------------------------------------
// ViewModel

public class MyViewModel
{
    public string SelectedYear { get; set; }
    public IEnumerable<Year> Years { get; set; }

    public string SelectedCourse { get; set; }
    pulbic IEnumerable<Course> Courses { get; set; }

    public string SelectedTeacher { get; set; }
    pulbic IEnumerable<Teacher> Teachers { get; set; }

    // This constructor is called when I create the ViewModel to pass for View in the "New" Controller method call.
    public MyViewModel()
    {
        Years = new List<Year>() {
            new Year(1, '2013'),
            new Year(2, '2012')
        };
        Courses = new List<Course>();
        Teachers = new List<Teacher>();
    }
// ----------------------------------------------------
// View
@model Application.Models.MyViewModel
<div id="content">
    @Html.LabelFor(m => m.SelectedYear)
    @Html.DropDownListFor(m => m.SelectedYear, new SelectList(Model.Years, "ID", "Description"))
    @Html.Partial("Courses", Model)
    @Html.Partial("Teachers", Model)
</div>

// ----------------------------------------------------
// Courses PartialView
<div id="courses">
    @Html.LabelFor(m => m.SelectedCourse)
    @Html.DropDownListFor(m => m.SelectedCourse, new SelectList(Model.Course, "ID", "Name))
</div>

// ----------------------------------------------------
// Teachers PartialView
<div id="teachers">
    @Html.LabelFor(m => m.SelectedTeacher)
    @Html.DropDownListFor(m => m.SelectedTeacher, new SelectList(Model.Teachers, "ID", "Name))
</div>

// ----------------------------------------------------
// jQuery function - called when I change the selected year.
$('#SelectedYear').change(function() {
    $.get('@Url.Action("MethodToRefreshModel", "Controller")', $('#form').serialize(), function (view) {
            $('#content').replaceWith(view);
        });
    });});

// ----------------------------------------------------
// Controller method to refresh courses
public ActionResult MethodToRefreshCourses(MyViewModel model)
{
     model.Courses = 
        (from course in context.Courses
         where course.Year.ID == Convert.ToInt64(model.SelectedYear)
         select course).ToList();

     // Returning my view with the refreshed model
     return View("New", model);
}

// Controller method to refresh teachers
public ActionResult MethodToRefreshTeachers(MyViewModel model)
{
     model.Teachers = 
        (from teacher in context.Teachers
         where teacher.Course.ID == Convert.ToInt64(model.SelectedCourse)
         select teacher).ToList();

     // Returning my view with the refreshed model
     return View("New", model);
}

当我调用该方法刷新课程时,我的页面返回并显示正确的数据。当我选择一门课程来寻找老师时,在我的控制器中,我的模型似乎“丢失”了填充到另一种方法中的课程。任何人都知道发生了什么以及如何解决它?

谢谢你们!

4

1 回答 1

0

在您的 jquery 选择元素更改事件中,您正在绑定一个函数,该函数将调用一个仅返回单个值的函数(调试 actionmethod 并检查模型对象中的值),然后您将替换整个内容的内容 div ( $('#content').replaceWith(view);)。

您应该只将所需的控件绑定到整个内容 div。

于 2013-01-17T12:12:26.033 回答