加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – MVC3 Html.EditorFor在我的视图中不渲染任何东

发布时间:2020-12-16 10:00:56 所属栏目:asp.Net 来源:网络整理
导读:public class RegisterViewModel{ //Plain POCO Properties public RegisterModel RegisterModel { get; set; } //Meant to be used in SelectLists. public IEnumerableCityModel Cities { get; set; } public IEnumerableCountryModel Countries { get; se
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; }
}

以下是这些课程:

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; } 
}

以下是我在视图中调用RegisterViewModel的方法:

@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> 
}

是否导致此错误,因为我的ViewModel本身没有任何注释?我在哪里可以正式阅读这个?我在网上找到的只是偶尔的博客,但他们在网上提到了简单的模型,而不是那些与其他对象有关系的模型.典型的外国关键国家与人际关系.

有任何想法吗?

解决方法

视图模型RegisterViewModel没有任何“简单”属性,并且MVC框架不呈现复杂属性,除非我们告诉它如何呈现它们.我们必须为EditorFor帮助器创建DisplayFor和编辑器模板的显示模板.有关详细信息,请查看 ASP.NET MVC 2 Templates.另一个 link.

将EditorTemplates放在文件夹中

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

RegisterModel.cshtml

@model RegisterModel

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

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

...
...

CityModel.cshtml

@model CityModel

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

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

CountryModel.cshtml

@model CountryModel

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读