Leito87
Leito87

Reputation: 33

How to pass list from controller to view and display

I try get datetime value from user and return selected screening of movie. Controller passed list to view, but html doesn't change.

Contoller

public ActionResult GetScreenings()
            {
                return View();
            }
    [HttpPost]
    public ActionResult GetScreenings(string starteDateTime, string endDateTime, string movieTitle)
    {
        List<ScreeningDisplayDTO> screeningsList = new List<ScreeningDisplayDTO>();
        if (String.IsNullOrEmpty(movieTitle))
        {
            screeningsList = screeningDisplayService.GetScreeningByParametr(starteDateTime, endDateTime).ToList();
        }
        else
        {
            screeningsList = screeningDisplayService.GetScreeningByParametr(starteDateTime, endDateTime, movieTitle).ToList();

        }
        return View(screeningsList);

    }

View

 @if (Model != null && Model.Count() > 0)
                {
                    foreach (var screening in Model)
                    {
                        
                       <p class="offer-film-title">@screening.MovieTitle</p>
                    }
                }

Ajax

$.ajax({
        traditional: true,
        type: "POST",
        url: '@Url.Action("GetScreenings")',
        data: { 'starteDateTime': starteDateTime, 'endDateTime': endDateTime, 'movieTitle': movieTitle  },
        success: function () {
            window.location.href = data;
        }
    });

Thanks for help.

Upvotes: 3

Views: 292

Answers (1)

Serge
Serge

Reputation: 43860

If you want to use ajax to change your view , you have to use a partial view

<div id="partialView">
    
    <partial name="_PartialView" />
        
    </div>

and return this partial view from the action and render :

  success: function (result) {
             $("#partialView").html(result);
        }

Maybe it would be easier for you to use a submit button.

Upvotes: 1

Related Questions