我现在正在学习 MVC 3(代码优先),感谢 asp.net 上的一些很棒的教程和 stackoverflow 上的一些问题,我学到了很多东西,但我想不通。我正在尝试设置默认值。当有人提出问题时,默认情况下该问题的状态必须为 3(= 未回答)。多亏了这篇文章,我已经成功地设置了它:asp.net mvc 视图模型中的默认值, 但现在我正在尝试为多对多的关系做同样的事情。(在 QuestionModel 公共虚拟 ICollection 类别中)
有人可以帮我解决这个问题。
这是我的 QuestionModel (Question.cs)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace MvcDatabase.Models
{
public class Question
{
[Key]
public int QuestionID { get; set; }
[DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
[Display(Name="Datum")]
public DateTime Date { get; set; }
[Required(ErrorMessage="Naam is een verplicht veld.")]
[MaxLength(100, ErrorMessage="Naam kan maximum 100 karakters bevatten")]
[Display(Name="Naam")]
public string Author { get; set; }
[Required(ErrorMessage="E-mail is een verplicht veld.")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
public string MailAuthor { get; set; }
[Required(ErrorMessage="Onderwerp is een verplicht veld.")]
[MaxLength(100, ErrorMessage="Onderwerp kan maximum 100 karakters bevatten.")]
[Display(Name="Onderwerp")]
public string Title { get; set; }
[Required(ErrorMessage="Bericht is een verplicht veld.")]
[Display(Name="Bericht")]
public string Message { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Reaction> Reactions { get; set; }
[ForeignKey("Status")]
public int StatusID { get; set; }
public virtual Status Status { get; set; }
public Question()
{
StatusID = 3;
}
}
}
这是 CategoryModel (Category.cs)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace MvcDatabase.Models
{
public class Category
{
[Key]
public int CategoryID { get; set; }
[Required(ErrorMessage="Omschrijving is een verplicht veld.")]
[MaxLength(100, ErrorMessage = "Omschrijving kan maximum 100 karakters bevatten.")]
[Display(Name="Categorie omschrijving")]
public string Description { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
}
这是 DbContextModel (TestotheekDBContext.cs)
using System.Data.Entity;
namespace MvcDatabase.Models
{
public class TestotheekDBContext: DbContext
{
public DbSet<Status> Statuses { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Question> Questions { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<Reaction> Reactions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Question>()
.HasMany(qc => qc.Categories)
.WithMany(cq => cq.Questions)
.Map(m => m.MapLeftKey("QuestionID")
.MapRightKey("CategoryID")
.ToTable("QuestionsWithCategory"));
}
}
}
如果您需要控制器中的代码
public ActionResult Create()
{
return View(new Question());
}
//
// POST: /Default4/Create
[HttpPost]
public ActionResult Create(Question question)
{
var vraag = new Question();
vraag.Date = DateTime.Now;
vraag.Author = question.Author;
vraag.MailAuthor = question.MailAuthor;
vraag.Title = question.Title;
vraag.Message = question.Message;
if(ModelState.IsValid)
{
db.Questions.Add(vraag);
db.SaveChanges();
return RedirectToAction("Index","Home");
}
return View(question);
}
(没有测试return RedirectToAction)
编辑
这是我目前得到的结果: 正如您在第一部分(第一个表)的最后一个问题中看到的,没有类别(字段为空)这必须与该表中的所有其他问题相同类别,因为管理员必须分配一个(或多个)。