ASP.NET MVC3复选框下拉列表创建

时间:2013-07-01 14:45:01

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

我是asp.net MVC中的新手,我/我使用视图模型来弹出下拉列表和一组复选框。我使用SQL Server 2012,其中学生之间有很多关系 - 书籍;学生 - 城市。我需要为一名学生收集学生姓名,一个城市和许多书籍。

我有下一个问题:
1. 如何从数据库中获取值到StudentBookCityViewModel?
2. 如何在[HttpPost]创建方法中将值保存到我的数据库?

以下是代码:

MODEL

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

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 City {
    public int CityId { get; set; }
    public string CityName { get; set; }
    public bool IsSelected { get; set; }

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

查看模型

public class StudentBookCityViewModel {
    public string StudentName { get; set; }
    public IList<Book> Books { get; set; }

    public StudentBookCityViewModel()
    {
        Books = new[]
                    {
                        new Book {BookName = "Title1", IsSelected = false},
                        new Book {BookName = "Title2", IsSelected = false},
                        new Book {BookName = "Title3", IsSelected = false}
                    }.ToList();
    }

    public string City { get; set; }
    public IEnumerable<SelectListItem> CityValues
    {
        get
        {
            return new[]
                       {
                           new SelectListItem {Value = "Value1", Text = "Text1"},
                           new SelectListItem {Value = "Value2", Text = "Text2"},
                           new SelectListItem {Value = "Value3", Text = "Text3"}
                       };
        }
    }
}

上下文

public class EFDbContext : DbContext{
    public EFDbContext(string connectionString) {
        Database.Connection.ConnectionString = connectionString;
    }

    public DbSet<Book> Books { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<City> Cities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<Book>()
            .HasMany(x => x.Students).WithMany(x => x.Books)
            .Map(x => x.MapLeftKey("BookId").MapRightKey("StudentId").ToTable("StudentBooks"));

        modelBuilder.Entity<City>()
            .HasMany(x => x.Students).WithMany(x => x.Cities)
            .Map(x => x.MapLeftKey("CityId").MapRightKey("StudentId").ToTable("StudentCities"));
    }
}

控制器

public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create()
    {
        //I don't understand how I can save values to db

        context.SaveChanges();

        return RedirectToAction("Index");
    }

查看

@model UsingEFNew.ViewModels.StudentBookCityViewModel

@using (Html.BeginForm())
{
    <div id="ContactContainer">
        <div>Your Name:</div>
        <div>
            @Html.TextBoxFor(model => model.StudentName)
        </div>

        <div>Genre:</div>
        <div> 
            @Html.DropDownListFor(model => model.City, Model.CityValues)
        </div>
        <div>Books:</div>
        <div>
            @for (int i = 0; i < Model.Books.Count; i++)
            {
                <div>
                    @Html.HiddenFor(x => x.Books[i].BookId)
                    @Html.CheckBoxFor(x => x.Books[i].IsSelected)
                    @Html.LabelFor(x => x.Books[i].IsSelected, Model.Books[i].BookName)
                </div>
            }
        </div>
        <div>
            <input id="btnSubmit" type="submit" value="Submit" />
        </div>
    </div>
}

1 个答案:

答案 0 :(得分:2)

您可以直接在控制器

中从db获取值
    public ActionResult Create()
        {
                var vm = new StudentBookCityViewModel();
                using(var ctx = EFDbContext(connectionString))
                {
                var student = ctx.Students.First();
                vm.StudentName=student.Name;
    vm.Books = student.Books.ToList(); //make sure to call ToList() to load values from db
    ...
                }
                return View(vm);
            }
        [HttpPost]
    public ActionResult Create(StudentBookCityViewModel model)
    {

        //I don't understand how I can save values to db

        //here you have the model with all infomation from the form
       foreach(var book in model.Books){
        context.Books.First(b=>b.BookId == book.BookId).IsSelected = book.IsSelected;
}
        context.SaveChanges();

        return RedirectToAction("Index");
    }