使用mvc5 web应用程序向数据库插入多行
本文关键字:插入 数据库 mvc5 web 应用程序 使用 | 更新日期: 2023-09-27 18:03:01
我试图添加多行数据,并将其传递到数据库中的表,以及另一个项目到不同的表。下面是数据库的结构体:
User Table
userid
name
Movie Table
Movieid
userid
moviename
Cinema Table
Movieid
cinemaid
location
time
movietable与Cinema table有一对多的关系。用户表有一个,我有四个不同的视图模型与它们相关联:
UserViewModel
{
public int userid;
public string name;
}
MoviewViewModel
{
public int movieid;
public int userid;
public string moviename;
public List<CinemaViewModel> cinema;
}
CinemaViewModel
{
public int movieid;
public int cinemaid;
public string location;
public string time;
}
UserandMovieViewModel
{
public List<MoviewViewModel> movie;
public UserViewModel user;
}
我正在传递userandmoviewmodel从控制器创建到视图,并希望为用户和电影添加一个条目,但希望从该页面向电影数据库添加多个条目。当我添加一个单一的入口到电影院时,它工作得很好。然而,我想有能力添加多个条目到电影院表时,形式张贴。我试过下面的教程,但似乎不适合创建。
http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/编辑
给出null异常的代码 <ul id="movieEditor" style="list-style-type: none">
@foreach (CinemaViewModel cinemamodel in Model.UserandMovieViewModel.cinema) {
Html.RenderPartial("MovieEntryEditor", cinemamodel);
}
</ul>
<a id="addAnother" href="#">Add another</a>
EDIT2
创建控制器代码
[httpget]
public ActionResult Create()
{
UserViewModel usermodel = new UserviewModel();
List<MovieViewModel> moviemodel= new List<MovieViewModel>();
UserandMovieViewModel model = new UserandMovieViewmodel{user = usermodel, movie=moviemodel }
return View(model)
}
[httppost]
public ActionResult Create(UserandMovieViewmodel model)
{
IRepository<User> userrep = new ApplicationRepository<User>();
IRepository<Movie> userrep = new ApplicationRepository<Movie>();
IRepository<Cinema> userrep = new ApplicationRepository<Cinema>();
User user = null;
Movie movie = null;
Cinema cinema = null;
UserViewModel usermodel = model.usermodel;
MovieViewModel moviemodel= model.moviemodel;
CinemaViewModel cinemamodel = model.moviemodel.cinema;
if(ModelState.valid)
{
user = new user();
user.name = usermodel.name;
userrep.add(user);
movie = new movie();
movie.userid = user.userid; (gets from database as its autoappend)
movie.moviename = moviemodel.moviename;
movierep.Add(movie);
cinema = new cinema();
cinema.movieid = movie.movieid;
cinema.location = cinemamodel.location;
cinema.time = cinemamodel.time;
cinemarep.Add(cinema);
}
return View(model);
}
我已经从内存中编写了代码,因为我目前没有它。如有错误,请改正。
3编辑
局部视图
@model Application.ViewModels.CinemaViewModel
<li style="padding-bottom:15px">
<div style="width: 450px; float: left;">
<label class="location">
Location
</label>
<span style="margin-left: 26px;">
@Html.TextBoxFor(model => model.location)
@Html.ValidationMessageFor(model => model.location)
</span>
<span>
@Html.TextBoxFor(model => model.time)
@Html.ValidationMessageFor(model => model.time)
</span>
</div>
我怀疑你的列表为空,所以你可以尝试在构造函数中新建它:
public class MoviewViewModel
{
public MoviewViewModel(){
cinema = new List<CinemaViewModel>();
}
public int movieid;
public int userid;
public string moviename;
public List<CinemaViewModel> cinema;
}
public class UserandMovieViewModel
{
public UserandMovieViewModel(){
movie = new List<MoviewViewModel>();
}
public List<MoviewViewModel> movie;
public UserViewModel user;
}
编辑:你的观点:
<ul id="movieEditor" style="list-style-type: none">
@if(Model.UserandMovieViewModel.cinema.Any())
{
foreach (CinemaViewModel cinemamodel in Model.UserandMovieViewModel.cinema) {
Html.RenderPartial("MovieEntryEditor", cinemamodel);
}
}
else
{
@{ Html.RenderPartial("MovieEntryEditor", new CinemaViewModel()) };
}
</ul>
<a id="addAnother" href="#">Add another</a>