ASP.NET MVC3创建多对多

时间:2013-07-01 18:05:38

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

我使用的捆绑包:ASP.NET MVC3,SQL Server 2012,EF5。当我尝试将值保存到我的数据库中时,我只能在我的数据库中的Books表中保存Book的属性IsSelected,StudentName无法保存,并且不能将数据保存到数据库中的StudentBooks表中,其中包含StudentId和BookId,用于多个键的键。 - 很多关系,任何想法?

public class CheckboxController : Controller
{
    EFDbContext context = new EFDbContext(ConfigurationManager.ConnectionStrings[1].ConnectionString);

    //
    //GET: /Checkbox/

    public ActionResult Index()
    {
        ViewModelData vmd = new ViewModelData();
        List<Book> bookList = new List<Book>();

        using (EFDbContext te = new EFDbContext(ConfigurationManager.ConnectionStrings[1].ConnectionString))
        {
            var student = te.Students.First();
            vmd.StudentName = student.StudentName;

            var data = te.Books.ToList();
            foreach (var d in data) {
                Book book = new Book();
                book.BookName = d.BookName;
                book.IsSelected = false;
                book.BookId = d.BookId;
                bookList.Add(book);
            }
        }

        vmd.Books = bookList;

        return View(vmd);
    }

    //
    //GET: /Checkbox/

    [HttpPost]
    public ActionResult Index(ViewModelData vm)
    {
        foreach (var book in vm.Books) {
            context.Books.First(x => x.BookId == book.BookId).IsSelected = book.IsSelected;
        }


        context.SaveChanges();
        return View(vm);
    }
}

public class ViewModelData
{
    public string StudentName { get; set; }
    public List<Book> Books { get; set; }
}

查看

    @model UsingEFNew.Controllers.ViewModelData

@{
    ViewBag.Title = "Example";
}

@using (Html.BeginForm("Index", "Checkbox", null, FormMethod.Post))
{
<div>Your Name:</div>
    <div>
        @Html.TextBoxFor(model => model.StudentName)
    </div>

    for (int i = 0; i < Model.Books.Count; i++) {
    @Html.CheckBoxFor(m => Model.Books[i].IsSelected)
    @Html.DisplayFor(m => Model.Books[i].BookName)
    @Html.HiddenFor(m => Model.Books[i].BookId)
    @Html.HiddenFor(m => Model.Books[i].BookName)
@:</br>
}

}

我的模特

public class Book {
    public int BookId { get; set; }
    public string BookName { get; set; }
    public bool IsSelected { get; set; }

    public ICollection<Student> Students { get; set; }
}

public class Student {
        public int StudentId { get; set; }
        public string StudentName { get; set; }

        public ICollection<Book> Books { get; set; }
        public ICollection<City> Cities { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

在您的[HttpPost]索引中,您不会从ViewDataModel获取学生名称并将其推送到任何学生实体。

[HttpPost]
public ActionResult Index(ViewModelData vm)
{
    foreach (var book in vm.Books) {
        context.Books.First(x => x.BookId == book.BookId).IsSelected = book.IsSelected;
    }        

    var student = context.Students.First();
    student.Name = vm.StudentName;//now I've actually changed the student entity and changes will be pushed to database on Save()

    context.SaveChanges();
    return View(vm);
}

然而,看起来你并没有真正利用他们之间的关系。你只是抓住第一个学生,而不是使用从书本到学生的关系。我在这里做了同样的事情,只是为了演示将视图模型中的值映射到您的实体。