0

I'm use bundle of: ASP.NET MVC3, SQL Server 2012, EF5. When I try to save values into my database, I can save only Book's property IsSelected in Books table in my database, StudentName cant saved, and cant saved datas into my StudentBooks table in database, whach contains StudentId and BookId, keys for many-to-many relationships, any ideas?

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; }
}

View

    @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>
}

}

My Models

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; }
    }
Rakstit
  • 57
  • 1
  • 13
  • Show me your Book model and your Student model – Greg Jul 01 '13 at 18:33
  • There are a number of issues here. Firstly, your ViewModel should be using a Student object, not just the name. Secondly, the way you are iterating over the collection in your view is incorrect which is why the bindings aren't correct. Send me your project and I will fix it. – Greg Jul 02 '13 at 17:33
  • I was resolve that problem, thank you – Rakstit Jul 05 '13 at 03:46

1 Answers1

0

In your [HttpPost]Index you're not take the student Name from you ViewDataModel and pushing it into any Student entity.

[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);
}

It doesn't look like you're really leveraging the relationship between them however. You just grab the first student rather than using the relationship from book to student. I have done the same here just to demonstrate mapping the value from your view model into your entity.

AaronLS
  • 34,709
  • 17
  • 135
  • 191
  • Thank you for solution, but it's only update my Books table and update first row in Students table in my db, thats not what I need. – Rakstit Jul 01 '13 at 19:06
  • Well you only display a single student name on the form, and you only retrieve a single student name from the database. You loop through the books but not through the students. – AaronLS Jul 01 '13 at 19:49
  • It would be best to have dedicated page for students and display Edit links to the student page. Otherwise you would do something very complex like this: http://stackoverflow.com/questions/4646340/asp-net-mvc-master-detail-entry-form – AaronLS Jul 01 '13 at 19:50
  • Thank you, but I guess find solution myself) – Rakstit Jul 01 '13 at 20:56