开发者

MVC3 Html.EditorFor isn't rendering a single thing in my View

public class RegisterViewModel
{
    //Plain POCO Properties
    public RegisterModel RegisterModel { get; set; }

    //Meant to be used in SelectLists.
    public IEnumerable<CityModel> Cities { get; set; }
    public IEnumerable<CountryModel> Countries { get; set; }
}

Here are those classes:

public class RegisterModel
{
    [Required]
    [Display(Name = "Usuario")]
    public string UserName { get; set; }

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

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

    [DataType(DataType.Password)]
    [Display(Name = "Confirme Su Contraseña")]
    [Compare("Password", ErrorMessage = "Sus contraseñas no son las mismas.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "Nombre")]
    public string Nombre { get; set; }

    [Required]
    [Display(Name = "Apellido")]
    public string Apellido { get; set; }

    [Required]
    [Display(Name = "Direccion")]
    public string Address { get; set; }

    [Required]
    [Display(Name = "Telefono Fijo")]
    public string Telephone { get; set; }

    [Required]
 开发者_开发百科   [Display(Name = "Celular")]
    public string MobilePhone { get; set; }

    [Required]
    [Display(Name = "Fecha de Nacimiento")]
    public DateTime DateOfBirth { get; set; }

    [Required]
    [Display(Name = "Soy:")]
    public bool Sex { get; set; }

    [Required]
    [Display(Name = "Carnet")]
    public int Carnet { get; set; }
}

public class CityModel
{
    [HiddenInput(DisplayValue = false)]
    public int CityId { get; set; }

    [Required]
    [Display(Name = "Ciudad")]
    public string Name { get; set; }      
}

public class CountryModel
{
    [HiddenInput(DisplayValue = false)]
    public int CountryId { get; set; }

    [Required]
    [Display(Name = "Pais")]
    public string Name { get; set; } 
}

And here's how I call the RegisterViewModel in my view:

@model Foodiggly.WebUI.Models.RegisterViewModel

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>RegisterViewModel</legend>

        @Html.EditorForModel()

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset> 
}

Is this error caused because my ViewModel doens't have any annotations itself? Where can I formally read about this? All I've managed to find online is the occassional blog or two but they online mention simple models, not ones that have a relationship with other objects such as this. A typical foreign key country to person relationship.

Any ideas?


The view model RegisterViewModel doesn't have any "simple" properties and the MVC framework doesn't render complex properties unless we tell it how to render those. We have to create display templates for DisplayFor and editor template for EditorFor helpers. Check the ASP.NET MVC 2 Templates for details. Another link.

Place your EditorTemplates in folders

~/Views/Shared/EditorTemplates
or
~/Views/ControlerName/EditorTemplates

RegisterModel.cshtml

@model RegisterModel

@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)

@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)

...
...

CityModel.cshtml

@model CityModel

@Html.LabelFor(model => model.CityId)
@Html.EditorFor(model => model.CityId)

@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)

CountryModel.cshtml

@model CountryModel

@Html.LabelFor(model => model.CountryId)
@Html.EditorFor(model => model.CountryId)

@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)


I think you are right in that your ViewModel should have the annotations. In my MVC project, I annotate the ViewModel and everything works fine.

Keep in mind that your View knows nothing at all about your Data Model - it only knows your View Model. :-)

edit:

[DataMember]
public class User {
public string PhoneNumber 
{ ... }

}




//[ViewModel]
public class ViewUser  {

[Required, RegularExpression(@"^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$", ErrorMessage="Please enter a valid 10 digit phone number")]
public string ViewPhoneNumber {
}

}


View page

<div class="editor-field">
     @Html.EditorFor(model => model.Phone1)
     @Html.ValidationMessageFor(model => model.Phone1, "Please enter a valid phone number")
</div>

Does that help?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜