保存新创建的对象ASP.NET MVC

时间:2013-04-16 21:54:24

标签: c# asp.net-mvc entity-framework repository

我正在使用EF Code First,这是我的类,它引用了一个教授对象。

public class Mark
    {
        public int MarkId { get; set; }
        [Required(ErrorMessage = "Please provide Grade")]
        [Display(Name = "Grade Value")]
        public int MarkValue { get; set; }

        [Required(ErrorMessage = "Please provide grade type")]
        [Display(Name = "Grade Type")]
        public string Type { get; set; }

        [Required(ErrorMessage = "Please provide Subject")]
        [Display(Name = "Subject")]
        public virtual Subject Subject { get; set; }
        public virtual User User { get; set; }
        public virtual Professor Professor { get; set; }


    }

在我看来,我有一个下拉菜单来选择一个放置标记的教授。 我这样实现了它:

//controller
 public MarksController() : this(new MarkRepository())
        {
            IProfessorRepository professorsRepo = new ProfessorRepository();
            List<Professor> professors = professorsRepo.All.ToList<Professor>();

            List<SelectListItem> items = new List<SelectListItem>();
            foreach (Professor p in professors)
            {
                items.Add(new SelectListItem 
                { 
                    Text = String.Concat(p.FirstName, " ", p.LastName), Value = p.ProfessorId.ToString() 
                });
            }

            items.Add(new SelectListItem
            {
                Text = "Please select Professor", Value = "0", Selected = true
            });

            ViewBag.Professors = items;
        }
//view create or edit
<div class="editor-label">
    @Html.LabelFor(model => model.MarkValue)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.MarkValue)
    @Html.ValidationMessageFor(model => model.MarkValue)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Type)
    @Html.ValidationMessageFor(model => model.Type)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Professor)
</div>

<div class="editor-field">
    @Html.DropDownList("Professors");
</div>

是的,我知道Controller中有一个名为Create的元文件,它上传存储库方法InsertOrUpdate。

[HttpPost]
        public ActionResult Create(Mark mark)
        {
            if (ModelState.IsValid) {
                markRepository.InsertOrUpdate(mark);
                markRepository.Save();
                return RedirectToAction("Index");
            } else {
                return View();
            }
        }


 public void InsertOrUpdate(Mark mark)
        {
            if (mark.MarkId == default(int)) {
                // New entity
                context.Marks.Add(mark);
            } else {
                // Existing entity
                context.Entry(mark).State = EntityState.Modified;
            }
        }

一切似乎都是逻辑,但如何在教授选中的情况下保存新创建的Mark对象? ASP.NET如何安排在Controller上传递给Create方法的对象?我应该如何将所选教授与我正在创建的商标相匹配?

1 个答案:

答案 0 :(得分:2)

看看这个,你需要一个模型中的ProfessorId。 Model binding dropdown selected value

public class Mark
{

    // this will hold the selected value

    public string ProfessorID { get; set; }

    public IEnumerable<SelectListItem> Professors
    { 
        get
        {
            return Proffessors                
                .Select(x => new SelectListItem {
                    Value = x.ProfessorId.ToString(),
                    Text = String.Concat(p.FirstName, " ", p.LastName)
                });
        } 
    }

 public IEnumerable<Professor> Proffessors     
  {
     get
     {
         IProfessorRepository professorsRepo = new ProfessorRepository();
          return professorsRepo.All;
     }
 }

  //the rest of your code
}

在视图中执行

@Html.DropDownListFor(
    x => x.ProfessorId, 
    new SelectList(Model.Professors, "Value", "Text")
)