-1

What I want is that when the schedule form is loaded for the first time (or without data passing), it shows a list of MovieName. When I click on one MovieName, its ID is sent back to controller as an input of a SQL query, then the result is passed back to the view.

Here are what I've done. But I think when I click on the ActionLink, the controller doesnt handle the data passed back as it is not HttpPost. Also, I dont know how to show the new data back in view. Please help!

ScheduleController.cs

[HttpGet]
public ActionResult Index()
    {          
        var schedules = db.Schedules.Include(s => s.Movie)
                .OrderByDescending(s => s.Movie.MovieName)
                .ToList();
        return View(schedules);
    }

[HttpPost]
public ActionResult Index(int MovieID)
    {
        //return ("Clicked");
        var schedules = (from s in db.Schedules
                         orderby s.ShowDate
                         select s).ToList();
        return View(schedules);
    }

Schedule/Index

@using Booking_Ticket_Management_System.Models;
@model IEnumerable<Schedule>

@{
   ViewBag.Title = "Schedule";
}
<h2>Choose movies</h2>
@using (Html.BeginForm())
{
<div>
        @foreach (Schedule schedule in @Model)
        {

                @Html.ActionLink(schedule.Movie.MovieName, "Index", "Schedule", new { MovieID = schedule.MovieID},null)

            <br />
        }
    <br />
</div>

}

0

2 Answers 2

2

Ideally listing and detail view are always separate. i suggest you to make both the view separate.

[HttpGet]
public ActionResult Index()
{          
    var schedules = db.Schedules.Include(s => s.Movie)
            .OrderByDescending(s => s.Movie.MovieName)
            .ToList();
    return View(schedules);
}

[HttpGet]
public ActionResult Movie(int id)
{
    //return ("Clicked");
    var schedules = (from s in db.Schedules
                     Where s.MovieId == id
                     orderby s.ShowDate
                     select s).ToList();
    return View(schedules);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, there are 2 tables: Movie and Schedule. The first index shows all MovieNames which have MovieID in the Schedule table. When a user clicks on a MovieName, it shows the Schedule of that movie. I dont want to separate them.
Conceptually you are right. but you can return only one model from controller to view. so either you will have to combine tables in one class act as a custom model and show result based on condition (if it is movie show movie list else show schedule list) or you will have to take separate view.
1

As you do not pass the movieId in your query Try this :

[HttpPost]
public ActionResult Index(int MovieID)
{
    //return ("Clicked");
    var schedules = (from s in db.Schedules
                     where s.MovieId==MovieId
                     orderby s.ShowDate
                     select s).ToList();
    return View(schedules);
}

But what I will suggest to you is to create another Action and another view to display only the movie clicked. And as you just want to Get not to modify the value inside the Database. You have to use the [HttpGet]

[HttpGet]
public ActionResult Detail(int MovieID)
{
    //return ("Clicked");
    var schedule = (from s in db.Schedules
                     where s.MovieId==MovieId
                     orderby s.ShowDate
                     select s).FirstOrDefault();
    return View(schedule);
}

And in your Detail view created

@using Booking_Ticket_Management_System.Models; @model Schedule

In your Index view you must change the ActionLink to:

 @Html.ActionLink(schedule.Movie.MovieName, "Detail", "Schedule", new { MovieID = schedule.MovieID},null

3 Comments

What I want to do is similar to this website: cgv.vn/en/sessiontimes Please take a look.
Then you must use Ajax. To load the result of your selection to a custom Div. But it is not what you asked? You will need to create a Partial view and then load it to a Div.make some research. I am sure you will get what you want. But regarding your question, this can be the answer
I think I have to use Ajax. Thank you for your answer.

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.