复选框无法正确绑定

时间:2013-08-09 10:26:43

标签: .net asp.net-mvc checkbox model-binding

我的程序出现以下问题: 我有一个User类,其中包含您可以遵循的一系列课程。这些课程包含一系列培训日。我从MySQL数据库中获取所有值。我是模型绑定我的User类。一切都是正确的模型绑定,但是当我提交我选择的训练日时,它不会将值更改为true。

更新 切换到视图模型使它在单击提交时发回一个空的视图模型。

类:

public partial class User
{       
    /*insert User Properties*/
    public virtual ICollection<Course> Courses { get; set; }
    /*methods*/
}

public partial class Course
{
/*insert Course Properties*/
public virtual ICollection<Trainingday> Trainingdays{ get; set; }
public IEnumerable<Trainingday> GetTrainingdays()
    {
        IEnumerable<Trainingday> listDays = (from l in Trainingdays
                                                   where l.IsSelected
                                                   select l);
        return listDays;
    }
}

public partial class Trainingday
{
    public int Id { get; set; }
    public bool IsSelected { get; set; }
    public DateTime Date { get; set; }
}

控制器:

private CourseViewModel model;
public ActionResult Course(User user, string code)
{
    Course course = user.GetCourse(code); /*retrieves the course for specified code*/
    model = new CourseViewModel();
    model.Date = course.Date;
    model.Title = course.Title;
    model.Description = course.Description;
    model.Trainingdays = course.Trainingdays;
    return View("Course", model);
}

[HttpPost]
public ActionResult Trainingdays(CourseViewModel courseModel)
{
    IEnumerable<Trainingday> trainingdays = courseModel.GetTrainingdays();
    return View("Trainingdays", trainingdays);
}

视图模型:

public class CourseViewModel
{
    [Display(Name = "Title")]
    public string Title { get; set; }
    [Display(Name = "Description")]
    public string Description { get; set; }
    [DataType(DataType.Date)]
    [Display(Name = "Date")]
    public DateTime? Date { get; set; }
    public virtual ICollection<Trainingday> Trainingdays{get;set;}
    public IEnumerable<Trainingday> GetTrainingdays()
    {
        IEnumerable<Trainingday> listDays = (from l in Trainingdays
                                                 where l.IsSelected
                                                 select l);
        return listDays;
    }
}

查看:

@model Models.Domain.CourseViewModel
@{
    ViewBag.Title = "Course";
}
@using (Html.BeginForm("Trainingdays","Course", new{Model}))
{
<table>
<tr><td>Date</td><td>Selection</td></tr>
@foreach (var item in Model.Trainingdays)
      {
          <tr>
              <td>
                  @Html.DisplayFor(x => item.Date)
              </td>
              <td>
                  @Html.CheckBoxFor(x => item.IsSelected)
              </td>
          </tr>
      }
</table>
       <input type="submit" value="Show selected trainingdays!" />
  }

当我点击提交时,我的所有值都保持不变。 我一直在看这个问题一段时间,而我从其他人那里找到的解决方案似乎没有帮助。

1 个答案:

答案 0 :(得分:1)

您的查看被强类型为@model Models.Domain.Course,但您的POST方法需要User。默认的Model Binder根据每个HTML Helper在模型中创建视图中的每个输入或任何其他HTML元素的类型,在服务器中构建整个模型。

您要发布Course而不是User,因此请更改控制器:

[HttpPost]
public ActionResult Trainingdays(Course course)
{
  /*Do Stuff here*/
  return View("Trainingdays", trainingdays);
}

如果您需要显示或发布任何其他属性,您应该创建一个ViewModel来封装它,例如:

 public class CourseViewModel
 {
    public string Code { get; set; }
    public Course course { get; set; }
    /*Other stuff*/
 }

然后在查看

中使用它
@model Models.Domain.CourseViewModel

并将其作为操作参数:

接收
[HttpPost]
public ActionResult DoStuffWithCourse(CourseViewModel course)
{
  /*Do Stuff here*/
}

这样默认的模型绑定器就能为你做好事!