1

I'm trying to fill a model that has a list property with data, however no matter what I try the list data that I define in the controller will not pass to my view.

I got a viewmodel that looks like this:

public class FilmsViewModel
{
    public Films film { get; set; }
    public List<FilmExhibitions> exhibitions { get; set; }

    public FilmsViewModel()
    {
        film = new Films();
        exhibitions = new List<FilmExhibitions>();
    }       
}

then a controller that adds some base data to it:

public class AdminController : Controller
{
    private IItemsRepository repository = new DbItemsRepository();

    // GET: Admin
    public ActionResult Index()
    {
        return View();
    }

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


    public ActionResult _AddFilm()
    {
        FilmsViewModel filmsViewModel = new FilmsViewModel();
        filmsViewModel.exhibitions.Add(new FilmExhibitions());
        filmsViewModel.exhibitions.Add(new FilmExhibitions());
        filmsViewModel.exhibitions[0].startTime = new DateTime(2017, 1, 1, 0, 0, 0);
        filmsViewModel.exhibitions[0].endTime = new DateTime(2017, 1, 1, 0, 0, 0);
        filmsViewModel.exhibitions[1].startTime = new DateTime(0001, 1, 1, 0, 0, 0);
        filmsViewModel.exhibitions[1].endTime = new DateTime(0001, 1, 1, 0, 0, 0);

        return PartialView(new FilmsViewModel());
    }

    [HttpPost]
    public ActionResult _AddFilm(FilmsViewModel filmViewModel)
    {
        repository.AddItem(filmViewModel.film);

        Items item = repository.GetItemByName(filmViewModel.film.name);


        return View();
    }

And finally my view that just doesn't want to accept this data:

@using Project_IHFF.Models
@model FilmsViewModel

@Html.ValidationSummary()
@using (Html.BeginForm("_AddFilm"))
{
    <label>Name: </label>
    @Html.TextBoxFor(x => x.film.name, new { @class = "form-control"})
    <br />
    <label>Description:</label>
    @Html.TextBoxFor(x => x.film.description, new { @class = "form-control" })
    <br />
    <label>Location: </label>
    @Html.TextBoxFor(x => x.film.location, new { @class = "form-control" })
    <br />
    <label>Price: </label>
    @Html.TextBoxFor(x => x.film.price, new { @class = "form-control" })
    <br />
    <label>Directors:</label>
    @Html.TextBoxFor(x => x.film.director, new { @class = "form-control" })
    <br />
    <label>Actors: </label>
    @Html.TextBoxFor(x => x.film.actors, new { @class = "form-control" })
    <br />
    <label>Capacity: </label>
    @Html.TextBoxFor(x => x.film.capacity, new { @class = "form-control" })
    <br />
    <br />
    for (var i = 0; i < Model.exhibitions.Count(); i++)
    {
        @Html.HiddenFor(x => x.exhibitions[i].startTime);
        @Html.HiddenFor(x => x.exhibitions[i].endTime);
        @Html.HiddenFor(x => x.exhibitions[i].id);
    }

    <label>First Exhibition</label><br />
    <label>Starting Time:</label>
    @Html.TextBoxFor(x => x.exhibitions[0].startTime, new { @class = "form-control" })

    <br />
    <label>Ending Time:</label>
    @Html.TextBoxFor(x => x.exhibitions[0].endTime, new { @class = "form-control" })
    <br />
    <br />
    <div id="HiddenDiv">
        <label>Second Exhibition (Optional)</label>
        <br />
        <label>Starting Time:</label>
        @Html.TextBoxFor(x => x.exhibitions[1].startTime, new { @class = "form-control", id = "HiddenTextBox" })
        <br />
        <label>Ending Time:</label>
        @Html.TextBoxFor(x => x.exhibitions[1].endTime, new { @class = "form-control", id = "HiddenTextBox" })
        <br />
    </div>


    <input type="button" id="VisibilityButton" name="show" value="+ Add second exhibition" onclick="showDiv()" class="btn btn-primary" />

    <script>
        function showDiv() {
            document.getElementById('HiddenDiv').style.display = "block";
            document.getElementById('VisibilityButton').value = "- Remove second exhibition";
            document.getElementById('VisibilityButton').onclick = function () { hideDiv() };
        }

        function hideDiv() {
            document.getElementById('HiddenDiv').style.display = "none";
            document.getElementById('VisibilityButton').value = "+ Add second exhibition";
            document.getElementById('VisibilityButton').onclick = function () { showDiv() };
        }
    </script>

    <input type="submit" value="Submit" class="btn btn-primary" />

Does anyone have an idea what I'm doing wrong here? I'm completely lost at this point.

3
  • 1
    You're returning PartialView(new FilmsViewModel()) from your _AddFilm controller action, you should return PartialView(filmsViewModel) instead. Commented Jan 6, 2017 at 16:46
  • Ooh wow, I can't believe I missed that. Thanks a lot! Commented Jan 6, 2017 at 16:48
  • If ypu expect the value to be populated on AddFilm action, then return PartialView(filmsViewModel) Commented Jan 6, 2017 at 16:48

1 Answer 1

2

In your _AddFilm controller you are returning PartialView(new FilmsViewModel()); instead of the FilesViewModel which you previously created.

I believe your code should look like this

public ActionResult _AddFilm()
{
    FilmsViewModel filmsViewModel = new FilmsViewModel();
    filmsViewModel.exhibitions.Add(new FilmExhibitions());
    filmsViewModel.exhibitions.Add(new FilmExhibitions());
    filmsViewModel.exhibitions[0].startTime = new DateTime(2017, 1, 1, 0, 0, 0);
    filmsViewModel.exhibitions[0].endTime = new DateTime(2017, 1, 1, 0, 0, 0);
    filmsViewModel.exhibitions[1].startTime = new DateTime(0001, 1, 1, 0, 0, 0);
    filmsViewModel.exhibitions[1].endTime = new DateTime(0001, 1, 1, 0, 0, 0);

    return PartialView(filmsViewModel);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.