我对使用ASP.NET MVC编程C#相对较新,我有一个问题。我正在构建某种电影添加节目功能。我目前得到了代码,我可以根据选定的日期获得所有CinemaShows HallID
,并通过[Post]
发送给控制器。现在我已经提供了我需要的信息在ViewModel
的列表中,我想将其与局部视图一起传递,以便在调用POST方法时,正确的信息显示在局部视图中。我将显示视图和控制器代码。
@using IVH7A5.WebUI.Models;
@using IVH7A5.Domain.Entities;
@model addShowViewModel
@{
ViewBag.Title = "addShow";
}
<div style="float:inherit">
@using (Html.BeginForm("getDateShows", "Show")) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class=" form-group">
<label class="control-label col-md-2">Datum</label>
<div class="col-md-10">
<input type="date" class="form-control" id="datum" name="datum" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Toepassen</button>
</div>
</div>
</div>
}
</div>
<div>
@{Html.RenderPartial("_ShowsByCinema");}
</div>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Show</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-2">Zalen</label>
<div class="col-md-10">
<select class="form-control" name="cinemanumbers" id="cinemanumbers">
@foreach (Cinema cin in Model.Cinemas) {
<option value="@cin.cinemaNumber">@cin.cinemaNumber</option>
}
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Zalen</label>
<div class="col-md-10">
<select class="form-control" name="film" id="film">
@foreach (Movie movie in Model.Movies) {
<option value="@movie.MovieTitle">@movie.MovieTitle</option>
}
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">3D</label>
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Shows.ThreeD)
@Html.ValidationMessageFor(model => model.Shows.ThreeD, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Starttijd</label>
<div class="col-md-10">
@Html.EditorFor(model => model.Shows.StartTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Shows.StartTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
注意我确实有一些荷兰标签,因为我看到我想要传递给局部视图的其他ViewModel
。我的猜测是RenderPartial()
行需要一些工作。
我的部分观点:
@using IVH7A5.WebUI.Models;
@using IVH7A5.Domain.Entities
@model List<ShowsByCinema>
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-8 col-md-offset-2 col-sm-12 movie-film-times">
<h3>Zalen en tijden:</h3>
<div class="movie-by-day">
@foreach (ShowsByCinema x in Model) {
<div class="row">
<div class="col-xs-3 movie-day">
<strong>@x.cinemaNumber</strong>
</div>
<div class="col-xs-9">
@foreach (Show s in x.Shows) {
@s.StartTime.ToString("HH:mm") @:- @s.StartTime.AddMinutes(s.movie.MovieLength)
}
</div>
</div>
}
</div>
</div>
</div>
当然还有控制器:
using IVH7A5.Domain.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IVH7A5.Domain.Entities;
using IVH7A5.WebUI.Models;
namespace IVH7A5.WebUI.Controllers {
public class ShowController : Controller {
private ICinemaRepository cinemaRepository;
private IMovieRepository movieRepository;
private IShowRepository showRepository;
public ShowController(ICinemaRepository cinemaRepo, IMovieRepository movieRepo, IShowRepository showRepo) {
cinemaRepository = cinemaRepo;
showRepository = showRepo;
movieRepository = movieRepo;
}
// GET: Show
public ActionResult Index() {
var model = new addShowViewModel();
model.Cinemas = cinemaRepository.cinemas;
model.Movies = movieRepository.Movies;
return View("addShow", model);
}
[HttpPost]
public ActionResult getDateShows(FormCollection form) {
List<ShowsByCinema> ShowsByCinemaList = new List<ShowsByCinema>();
DateTime date = Convert.ToDateTime(form["datum"]);
foreach (Cinema c in cinemaRepository.cinemas) {
ShowsByCinemaList.Add(new ShowsByCinema {
cinemaNumber = c.cinemaNumber,
Shows = showRepository.getshowsByCinemaAndDate(date, c.cinemaID)
});
}
return PartialView("_ShowsByDayPartial", ShowsByCinemaList);
}
}
}
所以,给出一个快速摘要:我想知道在调用POST方法ViewModel
时使用正确的getDateShows
呈现局部视图。我对编程很新,如果它包含与代码约定相矛盾的代码,我想道歉。我希望有人能给我解决我的问题。如果需要更多信息/代码,请随时询问。