2
我很新MVC。我正在使用interface
作爲我的模型的屬性。MVC3模型綁定與城堡
我注意到我的Data Annotation Attributes
被忽略。我也在提交表單時出錯:
無法創建接口的實例。
很快我就想通了,我將不得不使用定製ModelBinder
我有很難搞清楚需要的ModelBinder
的CreateModel
方法,我有以下RegistrationModel
裏面做什麼:
public class RegistrationModel
{
#region Properties (8)
public string Email { get; set; }
public string FirstName { get; set; }
public Gender Gender { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public string PasswordConfirmation { get; set; }
public IPlace Place { get; set; }
public string Username { get; set; }
#endregion Properties
}
這裏是IPlace
接口和實現:
public interface IPlace
{
#region Data Members (7)
string City { get; set; }
string Country { get; set; }
string ExternalId { get; set; }
Guid Id { get; set; }
string Name { get; set; }
string Neighborhood { get; set; }
string State { get; set; }
#endregion Data Members
}
public class Place : IPlace
{
#region Implementation of IPlace
public Guid Id { get; set; }
[StringLength(100, ErrorMessage = "City is too long.")]
public string City { get; set; }
[StringLength(100, ErrorMessage = "Country is too long.")]
public string Country { get; set; }
[StringLength(255, ErrorMessage = "External ID is too long.")]
public string ExternalId { get; set; }
[Required(ErrorMessage = "A name is required.")]
[StringLength(450, ErrorMessage = "Name is too long.")]
[DisplayName("Location")]
public string Name { get; set; }
[StringLength(100, ErrorMessage = "Neighborhood is too long.")]
public string Neighborhood { get; set; }
[StringLength(100, ErrorMessage = "State is too long.")]
public string State { get; set; }
#endregion
}
在這裏使用界面有什麼意義?擺脫它,並使用類「地方」 – 2012-04-04 11:18:56