简体   繁体   中英

Asp.net Core MVC - Data retrieval question

I'm new to Asp.net core and am having some trouble trying to think my way through this problem:

I have 2 controllers: a "Course" controller and a "Professor" controller.

When users see entries listed in the Professor model, and they hits details, it (as usual) calls the action in the "Professor" controller called "details".

the Details action, when called, displays all of the classes a professor teaches. After displaying these classes, users can click on a course.

And here is where I'm stuck: once users click on a course, I need to call a function in the "Course" controller to display the name of the course that users clicked on (along with other relevant information to the course - such as the department of the course). I am having a hard time figuring out how to "save" the name of this course the user clicks on, since they're just clicking on a button. How can I go about doing this?

Here is some code to put this all into perspective: (ignore my query strings - they're poorly formatted and need to be in a stored procedure)

The details action in my Professor Controller:

   public IActionResult Details(int? id)
        {
            TempData["ID"] = id;

            string connectionString = " ";

            if (id == null)
            {
                return NotFound();
            }

            string queryString = "SELECT ProfessorModel.FirstName, CourseModel.CourseName FROM ProfessorModel LEFT OUTER JOIN CourseAssignment ON ProfessorModel.ProfessorID = CourseAssignment.ProfessorID LEFT OUTER JOIN CourseModel ON CourseModel.CourseID = CourseAssignment.CourseID WHERE ProfessorModel.ProfessorID=@profID";

            var courseModel = new List<CourseModel>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Parameters.AddWithValue("profID", id);

                connection.Open();
                SqlDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    var course = new CourseModel();
                    course.SubjectName = rdr["CourseName"].ToString();

                    courseModel.Add(course);
                }

            }

            // var professorModel = await _context.ProfessorModel
            // .FirstOrDefaultAsync(m => m.ProfessorID == id);
            if (courseModel == null)
            {
                return NotFound();
            }
            
            return View(courseModel);          
        }

The "details" view that displays all the classes a professor teaches:

<h1>
    @foreach (var course in Model)
    {
    <a asp-controller="Course" asp-action="Index"> @course.SubjectName </a>
    <br />
    }
</h1>

The index action in my "Course" controller (i need to figure out a way to display relevant info based on the specific class users clicked on previously)

   public async Task<IActionResult> Index()
        {
            var ID = TempData["ID"];
            Console.WriteLine(ID);

            string connectionString = " ";
            string queryString = "SELECT * FROM CourseModel WHERE CourseID=@id";

            var model = new List<CourseModel>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Parameters.AddWithValue("id", ID);

                connection.Open();
                SqlDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    var course = new CourseModel();
                    course.SubjectName = rdr["SubjectName"].ToString();

                    model.Add(course);
                }

            }

            return View(model);
        }

Any ideas? I feel like I'm overcomplicating it and would appreciate some suggestions!

All you need to do is add the information required to link to the Course in your <a> tag. Assuming your course has as an Id property that is its primary key, you would do something like this.

@foreach (var course in Model)
{
    <a asp-controller="Course" asp-action="Details" asp-route-id="@course.Id"> @course.SubjectName </a>
}

And then have the Details method (or you could name the page and method anything you wanted) accept that parameter id .

public async Task<IActionResult> Details(int id) { ... }

You can provide any parameters you need for a view/method using asp-route-{parameterName}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM