数字标识值的MultiSelectList验证

时间:2013-06-10 16:39:43

标签: asp.net-mvc entity-framework ef-code-first

我有一个MultiSelectList,其dataValueField是代码隐藏模型中的数字,dataTextField字段是字符串。

当我在生成的html select元素中选择多个值时,我会收到验证错误,指出字段must be a number。这是有道理的,因为支持字段是一个整数,当您选择多个条目时,建筑物的ID值使用逗号连接。这将是一个什么样的工作?感谢。

模型如下。

// Selected buildings are stored in this table.
public class ClientSelectedBuildings
{
    public int ClientSelectedBuildingsId { get; set; }
    // ...
    [Display(Name = "Please select the buildings under consideration.")]
    public int? BuildingId { get; set; }
}

// Building list is retrieved from this table.
public class Buildings
{
    public int BuildingsId { get; set; }
    // ...
    [StringLength(255)]
    public string BuildingName { get; set; }
}

我的观点如下:

@model TheApplication.Models.ClientSelectedBuildings
    <div class="outer">
        <div class="inner">
            @Html.LabelFor(t => t.BuildingId)
            @Html.ListBoxFor(t => t.BuildingId, (MultiSelectList)ViewBag.Buildings, new { size = "4" })
            @Html.ValidationMessageFor(t => t.BuildingId)
        </div>
    </div>

1 个答案:

答案 0 :(得分:2)

问题是您的域模型只允许一个BuildingId,但表单将尝试通过列表框发送多个。

这是一个完美的示例,您的域模型与视图模型不完全匹配。 Domain和View每个都有不同的关注点,在非常非常基本的CRUD情况之外,你会发现表单视图总是需要一个单独的模型。

您将无法直接绑定到ClientSelectedBuildings(没有自定义模型绑定器)。相反,绑定到中间模型,然后可以映射到多个ClientSelectedBuildings。

// Here's the model submitted from the view. This will have to be mapped to domain
// entities.
public class FormModel
{
    // ... Other form fields ...

    public int[] BuildingIds { get; set;

    // ... Other form fields ...
}


// Example controller action that processes the form input.
[HttpPost]
public ActionResult MyPostAction(FormModel input)
{
    if (ModelState.IsValid)
    {
        // Loop all submitted building ids, map the data into domain entities.
        foreach(var buildingId in input.BuildingIds)
        {
            // Create the domain entity.
            var selected = new ClientSelectedBuildings
            {
                ClientSelectedBuildingsId = ... Wherever this comes from ...
                BuildingId = buildingId;
            };

            // Add to the data repository.
            this.MyDbContext.ClientSelectedBuildings.Add(selected);
        }

        // Submit all changes to the data context.
        this.MyDbContext.ClientSelectedBuildings.SaveChanges();

        // ... Return redirect to success view ...
    }

    // ... Return error view ...
}