ASP.NET MVC 3 - 加载到ViewModel中的内容在回调后丢失

时间:2013-01-17 12:01:02

标签: jquery asp.net-mvc-3 asp.net-mvc-viewmodel

我创建了一个回调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);
}

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

谢谢大家!

1 个答案:

答案 0 :(得分:0)

在你的jquery select元素更改事件中,你绑定了一个函数,这个函数将调用一个只返回一个值的函数(debug actionmethod并检查哪些值在模型对象中),之后你要替换内容div的全部内容($('#content')。replaceWith(view);)。

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