How would my model be in MVC3 if I needed to reference a foreign key, for example CountryID?
Here's what I have so far Model and View:
public cl开发者_如何学JAVAass 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 = "The {0} must be at least {2} characters long.", 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 = "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 = "Sexo")]
public bool Sex { get; set; }
[Required]
[Display(Name = "Pais")]
public int Country { get; set; }
}
@model Foo.WebUI.Models.RegisterModel
@{
ViewBag.Title = "Register";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<div id="registerform">
<h2>Cree Su Cuenta</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<h3>Informacion de Usuario</h3>
<div class="editor-field">
@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.Password)
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.ConfirmPassword)
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<h3>Informacion Personal</h3>
<div class="editor-field">
@Html.LabelFor(model => model.Address)
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.Telephone)
@Html.EditorFor(model => model.Telephone)
@Html.ValidationMessageFor(model => model.Telephone)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.MobilePhone)
@Html.EditorFor(model => model.MobilePhone)
@Html.ValidationMessageFor(model => model.MobilePhone)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.DateOfBirth)
@Html.EditorFor(model => model.DateOfBirth)
@Html.ValidationMessageFor(model => model.DateOfBirth)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.Sex)
@Html.EditorFor(model => model.Sex)
@Html.ValidationMessageFor(model => model.Sex)
</div>
<div class="editor-field">
@Html.LabelFor(model => model.Country)
@Html.EditorFor(model => model.Country)
@Html.ValidationMessageFor(model => model.Country)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
</div>
Now, since my User will belong to a single country, in the database it has a foreign key reference to the Country table. In my model, I set the Country property to be of type int. Is this correct?
How would I correctly set this up so a dropdownlist is shown for choosing a country in the View but a numerical value is saved for persistance to the database?
Your CountryId can remain an int (or any type you use for your ids). The Select List can be initialized with the appropriate value field for the selection, and the type will match without the need for conversion.
Your model will have to be part of a viewmodel that provides to the view what is needed to make the drop down list working (mostly a SelectList ).
public class RegisterViewModel
{
public RegisterModel register { get; set; }
public SelectList SelectCountriesList { get; set; }
}
The initilisation of this ViewModel will depend on how you work in your controller.
精彩评论