如何处理ICollection<>领域

时间:2012-02-23 18:21:43

标签: c# asp.net-mvc-3 razor

所以我在处理此模型上的ICollection字段时遇到很大麻烦

这是我的模特

public class RegisterModel
{
    public RegisterModel() 
    {
        this.Servicios = new HashSet<Servicio>();
    }

    [Required(ErrorMessage="El nombre de usuario es requerido.")]
    [Display(Name = "Usuario")]
    public string UserName { get; set; }

    //[Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required(ErrorMessage = "La contraseña es obligatoria.")]
    [StringLength(100, ErrorMessage = "El {0} debe tener al menos {2} caracteres de longitud.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Contraseña")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirmar contraseña.")]
    [Compare("Password", ErrorMessage = "La contraseña y la confirmacion no coinciden.")]
    public string ConfirmPassword { get; set; }

    [Required(ErrorMessage="Seleccione los servicios que desea agregar al usuario")]
    [Display(Name="Servicio")]
    public ICollection<Servicio> Servicios { get; set; } //This field
}

现在我想要实现的是为Servicio创建一个EditorTemplate实际上我做了它,但问题是在主视图中它是RegisterModel的强类型我不知道如何传递每个来自ICollection的Servicio&lt;&gt;我的EditorTemplate我试过这样但是它不起作用

@model SodexoSAT.Models.RegisterModel

@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "No se Pudo crear la cuenta. Por favor corrija los errores e inténtelo de nuevo.")
<div>
    <fieldset>
        <legend>Información de la cuenta</legend>

        <div class="editor-label">
            @Html.LabelFor(m => m.UserName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.UserName)
            @Html.ValidationMessageFor(m => m.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Password)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.Password)
            @Html.ValidationMessageFor(m => m.Password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.ConfirmPassword)
            @Html.ValidationMessageFor(m => m.ConfirmPassword)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Servicios)
        </div>
        <div class="editor-field">
            <div>
                @Html.EditorFor(m => Model.Servicios) <--doesn't work
                @Html.EditorForModel(m => Model.Servicios) <--doesn't work
                @Html.EditorForModel(Model.Servicios) <--doesn't work too
            </div>
        </div>

        <p>
            <input type="submit" value="Crear Usuario" class="botonAtento" />
        </p>
    </fieldset>
</div>

}

1 个答案:

答案 0 :(得分:0)

替换

    <div class="editor-label">
        @Html.LabelFor(m => m.Servicios)
    </div>
    <div class="editor-field">
        <div>
            @Html.EditorFor(m => Model.Servicios) <--doesn't work
            @Html.EditorForModel(m => Model.Servicios) <--doesn't work
            @Html.EditorForModel(Model.Servicios) <--doesn't work too
        </div>
    </div>

@foreach(var service in m.Servicios)
{
    <div class="editor-label">
        @Html.LabelFor(service)
    </div>
    <div class="editor-field">
        <div>
            @Html.EditorFor(service) <--doesn't work
        </div>
    </div>
}

这样,Model.Services每个服务都会有一个编辑器(附带标签)。