asp.net-mvc如何在控制器上张贴的模型列表?

发布时间: 2017/3/25 10:44:56
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

还有 1 种将索要学术详细信息为︰

  • 毕业
  • 邮政 Graduation(Masters)
  • 专业资格。

到目前为止任何用户说用户 Id ="1"的3 个条目将创建在我 AcademicMaster 每个为学士,硕士 (毕业后) 和专业资格

我的数据库 AcademicMaster 表字段和数据模型︰

Id,Qualification(GraduationCourses),Acheievement,UserId

视图模型︰

public class AcademicViewModel
{
    public int Id { get; set; }
    public virtual Graduation     Graduation{ get; set; }
    public virtual PostGraduation PostGraduation{ get; set; }
    public virtual ProfessionalQualification ProfessionalQualification{ get; set; }
}

public class Graduation
{
    public string BachelorQualification { get; set; }
    public string BachelorAchievement { get; set; }
}
public class PostGraduation
{
    public string MasterQualification { get; set; }
    public string MasterAchievement { get; set; }
}
public class ProfessionalQualification
{
    public string ProfessionalQualifications { get; set; }
}

所以我认为,像这样︰

@model AcademicViewModel
@{
    IEnumerable<SelectListItem> graduationList = ViewBag.GraduationList;
    IEnumerable<SelectListItem> postGraduationList = ViewBag.PostGraduationList;
}
@using (Html.BeginForm())
{
    <div class="row">
        Bachelors
        @Html.DropDownListFor(model => model.Graduation.Qualification, graduationList)
    </div>
    @Html.TextAreaFor(model => model.Graduation.Achievement)

    <div class="row">
        MASTERS
        @Html.DropDownListFor(model => model.PostGraduation.Qualification, postGraduationList)
    </div>
    @Html.TextAreaFor(model => model.PostGraduation.Achievement)
    <div class="row">
        PROFESSIONAL QUALIFITCATION
        @Html.TextAreaFor(model => model.ProfessionalQualification.ProfessionalQualifications)
    </div>
    <input type="submit" value="Save">
}

这是我的控制器︰

[HttpPost]
public ActionResult MyController(AcademicViewModel model)
{
    //Actions
}

所以是我适当的视图模型结构以及如何在 AcademicMaster Table 中创建 3 个条目??

解决方法 1:

我会先说有一个表可能不最好的选择 (会发生什么,如果以后你开始添加附加的属性,可适用于 Graduation ,并非适用于 Professional -例如 YearOfGraduation -你最终可能,数量庞大的领域,其中许多可能有一定的 null 值。

然而,如果你想要一个表,然后至少添加另一个字段以使您可以标识如果数据关乎 GraduationPostGraduationProfessional 。在关联的数据模型为 AcademicMasters 表会

public class AcademicMaster
{
  public int ID { get; set; }
  public string Type { get; set; } // may be an enum?
  public string Qualification { get; set; }
  public string Achievement { get; set; }
  public int UserID { get; set; }
}

边注︰ 它可能是更好地使用 enumType 属性

public enum AcademicType
{
  Graduation,
  PostGraduation,
  Professional
}

似乎并没有任何必要对你目前的 GraduationPostGraduationProfessionalQualification 模型和视图模型应该是

public class AcademicViewModel
{
  public string GraduationQualification { get; set; }
  public string GraduationAchievement { get; set; }
  public string PostGraduationQualification { get; set; }
  public string PostGraduationAchievement { get; set; }
  public string ProfessionalAchievement { get; set; }
  public IEnumerable<SelectListItem> GraduationList { get; set; }
  public IEnumerable<SelectListItem> PostGraduationList { get; set; }
}

便笺︰ 它不清楚什么您当前 ProfessionalQualifications 属性是 — — 没有,被分配到 Qualification 字段或 Acheievement 数据库中的字段?因为您使用的视图模型,那么它应该包括 SelectList 的而使用 ViewBag

然后您的视图将是

@model AcademicViewModel
@using (Html.BeginForm())
{
  <h2>Graduation</h2>
  @Html.DropDownListFor(m => m.GraduationQualification, Model.GraduationList)
  @Html.TextAreaFor(m => m.GraduationAchievement)
  ... // repeat for PostGraduation and Professional
  <input type="submit" value="Save">
}

POST 方法将

[HttpPost]
public ActionResult MyController(AcademicViewModel model) // should be named Create?
{
  var userID = ....
  AcademicMaster graduation = new AcademicMaster
  {
    Type = AcademicType.Graduation,
    Qualification = model.GraduationAchievement,
    Achievement = model.GraduationAchievement,
    UserId = userID;
  };
  db.AcademicMasters.Add(graduation);
  // Repeat for PostGraduation and Professional
  db.SaveChanges();
  // redirect?
}
官方微信
官方QQ群
31647020