Asp.net mvc 4 使用从业务对象继承的视图模型进行模型验证

Asp.net mvc 4 使用从业务对象继承的视图模型进行模型验证,asp.net-mvc-4,Asp.net Mvc 4,在MVC4应用程序中,我有一些作为视图模型继承的业务对象。我想在视图模型中使用DataAnnotations,而不是在业务对象中指定它。那有可能吗 业务对象 public class Country { [Display(Name = "Country")] public virtual int CountryId { get; set; } public virtual string CountryName { get; set; }

在MVC4应用程序中,我有一些作为视图模型继承的业务对象。我想在视图模型中使用
DataAnnotations
,而不是在业务对象中指定它。那有可能吗

业务对象

 public class Country
    {
        [Display(Name = "Country")]
        public virtual int CountryId { get; set; }
        public virtual string CountryName { get; set; }
        public virtual string CountryCode { get; set; }
    }

    public class State : Country
    {
        [Display(Name = "State")]
        public virtual int StateId { get; set; }
        public virtual string StateName { get; set; }
        public virtual string StateCode { get; set; }
    }

    public class City : State
    {
        public virtual int CityId { get; set; }

        [Display(Name = "City")]
        public virtual string CityName { get; set; }
    }
查看模型

public class Regional
{
    public Regional()
    {
        Countries = new Collection<Country>();
        States = new Collection<State>();
        City = new City();
    }
    public Collection<Country> Countries { get; set; }

    public Collection<State> States { get; set; }

    [Required(ErrorMessage = "Required")]
    public City City { get; set; }
}
公共类区域
{
公共区域()
{
国家=新集合();
States=新集合();
城市=新城();
}
公共收集国家{get;set;}
公共集合状态{get;set;}
[必需(ErrorMessage=“必需”)]
公共城市城市{get;set;}
}
在上面的代码中,我使用的是城市属性的数据注释

  • 这里面临的问题是,即使我没有这个必需的注释,表单也会得到国家和州的验证,但不会得到城市名称的验证
  • 当我添加所需的注释时,它仍然只针对国家和州而不是城市名称进行验证
  • 有什么想法吗

    我的观点

    @using (Html.BeginForm(Actions.AddCity, Controllers.AdminUtility, FormMethod.Post))
    {
        <div class="alert-error">
            @Html.ValidationSummary()
        </div>
        <div class="row-fluid">
            <fieldset class="form-horizontal">
                <legend class="header">Add City</legend>
                <div class="control-group">
                    @Html.LabelFor(m => m.City.CountryId, new { @class = "control-label" })
                    <div class="controls">
                        @Html.DropDownListFor(m => m.City.CountryId, new SelectList(Model.Countries, "CountryId", "CountryName"),"", new { @class = "chosen", data_placeholder = "Choose Country..." })
                    </div>
                </div>
                <div class="control-group">
                    @Html.LabelFor(m => m.City.StateId, new { @class = "control-label" })
                    <div class="controls">
                        @Html.DropDownListFor(m => m.City.StateId,new SelectList(Model.States, "StateId","StateName"), "",new { @class = "chosen", data_placeholder = "Choose State..." })
                    </div>
                </div>
                <div class="control-group">
                    @Html.LabelFor(m => m.City.CityName, new { @class = "control-label" })
                    <div class="controls">
                        @Html.TextBoxFor(m => m.City.CityName)
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <input type="submit" />
                    </div>
                </div>
            </fieldset>
        </div>
    }
    
    @使用(Html.BeginForm(Actions.AddCity、Controllers.AdminUtility、FormMethod.Post))
    {
    @Html.ValidationSummary()
    添加城市
    @LabelFor(m=>m.City.CountryId,新的{@class=“control label”})
    @Html.DropDownListFor(m=>m.City.CountryId,新选择列表(Model.Countries,“CountryId”,“CountryName”),“”,新{@class=“selected”,data_placeholder=“Choose Country…”)
    @LabelFor(m=>m.City.StateId,新的{@class=“control label”})
    @Html.DropDownListFor(m=>m.City.StateId,新选择列表(Model.States,“StateId”,“StateName”),“”,新{@class=“selected”,data_placeholder=“Choose State…”)
    @LabelFor(m=>m.City.CityName,新的{@class=“control label”})
    @Html.TextBoxFor(m=>m.City.CityName)
    }
    
    您的验证表明该类是必需的。您希望CityName是必需的,因此在您的场景中,不,您必须在域模型上指定属性


    请注意,我认为您的域模型不是很清楚。城市、州和国家并不是相互继承的,它们只是相关的。一个国家在一个国家,它不是一个国家。。。此外,您应该始终验证域对象,因为它们可能来自多个源。然后,您的ViewModel将具有特定于该视图的附加验证--可能它具有一些用于ui处理的附加数据,这些数据不需要传递到您的域中,等等。

    您的验证是指该类是必需的。您希望CityName是必需的,因此在您的场景中,不,您必须在域模型上指定属性

    请注意,我认为您的域模型不是很清楚。城市、州和国家并不是相互继承的,它们只是相关的。一个国家在一个国家,它不是一个国家。。。此外,您应该始终验证域对象,因为它们可能来自多个源。然后,您的ViewModel将具有特定于该视图的附加验证——可能它具有一些用于ui处理的附加数据,这些数据不需要传递到您的域,等等