如果我需要引用外键(例如 CountryID),我的模型在 MVC3 中的表现如何?

Posted

技术标签:

【中文标题】如果我需要引用外键(例如 CountryID),我的模型在 MVC3 中的表现如何?【英文标题】:How would my model be in MVC3 if I needed to reference a foreign key, for example CountryID? 【发布时间】:2011-08-16 18:09:52 【问题描述】:

这是我目前拥有的模型和视图:

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 = "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>

现在,由于我的用户将属于一个国家/地区,因此在数据库中它具有对 Country 表的外键引用。在我的模型中,我将 Country 属性设置为 int 类型。这是正确的吗?

我该如何正确设置,以便在视图中显示一个下拉列表以选择一个国家,但保存一个数值以持久保存到数据库?

【问题讨论】:

【参考方案1】:

您的 CountryId 可以保持为 int(或您用于 id 的任何类型)。选择列表可以用适当的值字段来初始化选择,类型会匹配而不需要转换。

您的模型必须是视图模型的一部分,该视图模型向视图提供使下拉列表正常工作所需的内容(主要是 SelectList )。

public class RegisterViewModel

  public RegisterModel register  get; set; 

  public SelectList SelectCountriesList  get; set; 

这个 ViewModel 的初始化将取决于您在控制器中的工作方式。

【讨论】:

感谢您的努力,但这并不能回答我的问题。我的模型应该是'int''string'还是什么? @sergio:它应该仍然是一个 int。选择列表可以使用适当的值字段名称进行初始化,并且选择将具有适当的类型。

以上是关于如果我需要引用外键(例如 CountryID),我的模型在 MVC3 中的表现如何?的主要内容,如果未能解决你的问题,请参考以下文章

VARCHAR 列的自动规范化?

mysql外键创建失败原因

AngularJS RESTful 参数

如何在 phpmyadmin 中显示外键的内容

mysql外键仅引用复合主键的一部分

引用另一个模式的外键