我正在尝试添加多行数据并将其传递到数据库中的表以及另一个项目到另一个表。下面是数据库的结构:
User Table
userid
name
Movie Table
Movieid
userid
moviename
Cinema Table
Movieid
cinemaid
location
time
电影表与Cinema表有1对多的关系。用户表有一个我有四个不同的viewmodels关联它们:
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从控制器创建传递给视图,并希望为用户和电影添加一个条目,但是希望从该单个页面向Cinema数据库添加多个条目。当我向电影院添加一个条目时,它工作正常。但是我希望能够在发布表单时向影院表添加多个条目。我已经尝试了下面的教程,但这似乎不适用于创建。
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>
答案 0 :(得分:0)
我怀疑你的列表是null,所以你可以尝试在构造函数中新建它:
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>