0

I am relatively new to programming C# with ASP.NET MVC and I have a question. I am building some kind of cinema add show feature. I currently got the code where i can get all the CinemaShows with the right HallID based on a selected date (and send to the controller by a [Post]. Now I've put the information I need in a list of a ViewModel and I want to pass it along with the partial view so that when the POST method is called, the right info shows in a partial view. I will show the views and controllers code.

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

Note I do have some Dutch labels, as seen I use a different ViewModel that I want to pass to the partial view. My guess is that the RenderPartial() line needs some work.

My partial view:

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

And, of course, the controller:

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

So, to give a quick summary: I would like to know to render the partial view with the proper ViewModel when I call the POST method getDateShows. I am very new to programming in general and I would like to apologize if it contains code that is in contradiction with the code conventions. I hope someone can give me the solution to my problem. If any more information/code is required, feel free to ask.

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
  • You post method needs to redirect back to the GET method and pass the date (or better, use ajax to post and return a partial view to update the DOM). Note also your partial is `@model List` but the main view model is `@model addShowViewModel` so you need to pass the model to the partial `Html.RenderPartial("_ShowsByCinema", Model.Cinemas)` –  Apr 03 '15 at 00:33
  • @StephenMuecke , I am aware that the Viewmodel of my main view and the viewmodel of the partial are differend. But that is exactly the reason I am trying to create this partial view combination. I have some other ideas in the back of my head, but i hope I could make the current code work – ThomasWoerdeman Apr 03 '15 at 00:41
  • If you don't care about performance (i.e. don't want to use ajax) then you need to redirect to a method and construct a new view based on the date you pass to the POST method –  Apr 03 '15 at 00:47
  • @StephenMuecke I think I already have that method (ActionResult getDateShows) my main question now is : at the end of the method i have a filled List with the information i need. Now i want to return to the same view the post came from, but now with a partial view that uses that new List. This seemed the most logical option , other options i can try is adding the ShowsByCinema List to the addShowViewModel and then just simply show the information directly if the list is not empty but I figured a partial is probably less dirty. Thanks for helping by the way, I appreciate it – ThomasWoerdeman Apr 03 '15 at 01:01
  • No you don't. `getDateShows()` is a POST method. It is not constructing a new view. Your just trying to return a partial (which would be fine of you use ajax and then update the DOM with the returned html). You need to have a GET method that generates a view (not a partial) that has a parameter `DateTime date`, and in the POST method, redirect to it and pass the date, and construct your new view based on that date (of course you could just modify the current index method to accept an optional date) –  Apr 03 '15 at 01:07

0 Answers0