将Checkboxlist绑定到模型

时间:2015-08-10 15:31:49

标签: asp.net-mvc asp.net-mvc-5

我正在为一个自行车队做一个应用程序。我必须加入车手。为此,我希望用户选择骑行日期,选择该骑行点的值,然后列出团队成员的列表,用户选择哪个成员参与骑行。

在我发布表单之前一切正常。当我在控制器中调试我的模型时,de model中的Riders列表会计算4个车手(这是正确的,因为我在我的Db中有4个车手进行测试)但是当我检查列表时车手是空的。

任何人都可以帮助我,我不知道我做错了什么。

这是我使用的视图模型:

public class RiderViewModel
{

    public string Id { get; set; }
    public string FulllName { get; set; }
    public bool IsChecked { get; set; }
}


public class RideViewModel
{
    public string RideId { get; set; }
    [Display(Name = "Datum")]
    [DataType(DataType.Date)]
    [Required(ErrorMessage = "Datum is verplicht")]
    public DateTime Date { get; set; }
    [Display(Name = "Aantal punten")]
    [Required(ErrorMessage = "Een waarde voor het puntenaantal is verplicht")]
    public int Punten { get; set; }
    [Display(Name = "Leden")]
    public  List<RiderViewModel> Riders { get; set; }
}

这是我的控制器:

//POST Rides/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(RideViewModel model)
    {
        if (ModelState.IsValid)
        {
            var db = new ApplicationDbContext();
            var riders = new List<ApplicationUser>();

            foreach(RiderViewModel r in model.Riders)
            {
                var user = db.Users.FirstOrDefault(u => u.Id == r.Id);
                if(user == null)
                {
                    ModelState.AddModelError(string.Empty, "Er is een Fout opgetreden bij het selecteren van de leden. contacteer de systeembeheerder indien het probleem blijft bestaan");
                    return View(model);
                }
                riders.Add(user);
            }

            var ride = new Ride
            {
                Date = model.Date,
                Punten = model.Punten,
                Riders = riders
            };

            db.Rides.Add(ride);
            db.SaveChanges();

            return RedirectToAction("Index", "AdminPanel");
        }
        return View(model);
    }

这是观点:

using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Ride</h4>
    <hr />
    <div class="form-group">
        @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Punten, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Punten, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Punten, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Leden", htmlAttributes: new { @class = "control-label col-md-2"})
        <div class="col-md-10">
            @for (var i = 0; i < Model.Riders.Count(); i++)
            {
                <div class="col-md-10">
                    @Html.HiddenFor(model => model.Riders[i])                        
                    @Html.CheckBoxFor(model => model.Riders[i].IsChecked)
                    @Html.LabelFor(model => model.Riders[i].IsChecked, Model.Riders[i].FulllName)
                 </div>
            }
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

}

Thanx很多。

grtz Jeff

2 个答案:

答案 0 :(得分:1)

您可以使用EditorTemplate,只需在 EditorTemplates 文件夹中添加一个名为 RiderViewModel 的新文件:

             @model RiderViewModel

             <div class="col-md-10">
                @Html.HiddenFor(model => model.Id)                        
                @Html.CheckBoxFor(model => model.IsChecked)
                @Html.LabelFor(model => model.IsChecked, Model.FulllName)
             </div>

然后在视图中调用EditorFor:

             @Html.EditorFor(m => m.Riders)

答案 1 :(得分:1)

您对

的使用
app.get('/dashboard', isLoggedIn, function(req, res) {...}

正在为类型为@Html.HiddenFor(model => model.Riders[i]) 的复杂属性创建隐藏输入。如果您检查html,它将类似于

RiderViewModel

当您提交时,<input type="hidden" id="Riders_0_" name="Riders[0]" value="someAssembly.RiderViewModel" /> 会尝试将DefaultModelBinder设置为失败的值model.Riders[0],因此someAssembly.RiderViewModelmodel.Riders[0]

假设您要为ID属性生成隐藏输入,请将其更改为

null