Asp.net mvc 如何将数据发布到联接表-使用EF 5、ASP.NET MVC 4

Asp.net mvc 如何将数据发布到联接表-使用EF 5、ASP.NET MVC 4,asp.net-mvc,entity-framework,asp.net-mvc-4,join,entity-framework-5,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Join,Entity Framework 5,我是MVC4新手,到目前为止,我一直在从一个表检索数据并保存回同一个表。现在,我有来自联接表的数据,需要保存回相同的联接表。我创建了一个新模型并创建了get控制器,但是当我尝试保存时,我得到了一个错误。任何建设性的帮助都将不胜感激 型号 public partial class JoinClass { public string Name { get; set; } public int EEID { get; set; } publi

我是MVC4新手,到目前为止,我一直在从一个表检索数据并保存回同一个表。现在,我有来自联接表的数据,需要保存回相同的联接表。我创建了一个新模型并创建了get控制器,但是当我尝试保存时,我得到了一个错误。任何建设性的帮助都将不胜感激

型号

 public partial class JoinClass
    {

        public string Name { get; set; }
        public int EEID { get; set; }
        public string Category { get; set; }
        public int Points { get; set; }
        public string Programs { get; set; }
        public DateTime EntryDate { get; set; }
        public DateTime Quarter { get; set; }

    }
控制器

   public ActionResult Create()
   {               
       ViewBag.VBQuarter = new SelectList(db.Quarter2, "Id_Quarter2", "Quarter");
       ViewBag.VBEEID = new SelectList(db.Users2, "Id_Users2", "EEID");
       ViewBag.FK_Programs = new SelectList(db.Programs, "Id_Programs", "Programs");
       ViewBag.EntryDate = DateTime.Now;

       return View();           
    }

    //
    // POST: 
    [HttpPost]
    public ActionResult Create(JoinClass joinclass)
    {
        if (ModelState.IsValid)
        {
           db.JoinClass.Add(joinclass);
           db.SaveChanges();
           return RedirectToAction("Index");
        }
        return View(joinclass);
    }
查看

@model 2014_V4.Models.JoinClass

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) {
       <fieldset>
        <legend>Points</legend>
        <div class="editor-label">
          Quarter
        </div>
        <div class="editor-field">
            @Html.DropDownList("VBQuarter")

        </div>

        <div class="editor-label">
           User
        </div>
        <div class="editor-field">
            @Html.DropDownList("VBEEID")      
        </div>
        <div class="editor-label">
            Programs
        </div>
        <div class="editor-field">
            @Html.DropDownList("FK_Programs", String.Empty)

        </div>

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

        </div>
        <div class="editor-label">
           Entery Date
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.EntryDate, new {@Value = ViewBag.EntryDate, @readonly="readonly" })

        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
@model 2014\u V4.Models.JoinClass
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
要点
一刻钟
@Html.DropDownList(“VBQuarter”)
使用者
@Html.DropDownList(“VBEEID”)
程序
@Html.DropDownList(“FK_程序”,String.Empty)
要点
@EditorFor(model=>model.Points)
肠期
@TextBoxFor(model=>model.EntryDate,new{@Value=ViewBag.EntryDate,@readonly=“readonly”})

} @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
您的问题在JoinClass的构造函数中。您不应使用
new HashSet
初始化虚拟导航属性集合。实体框架使用自己的集合类型-
EntityCollection
。您根本不应该初始化导航属性集合


PS:使用带有视图的特殊viewmodels并将数据从viewmodels显式传递到实体模型是一种很好的做法。

当我尝试保存时,会出现一个错误
-所以可能会将错误内容粘贴到此处?很抱歉,我今天不在开发机器上。但是错误基本上是“JoinClass不属于模型”你也能提供你的视图代码吗?@Vahind我在我的原始问题中添加了视图。你使用EntityFramework吗?谢谢你的建议。你能给我举一个例子,说明你在视图中使用特殊视图模型的意思吗。