简体   繁体   English

Asp.net Core MVC - 数据检索问题

[英]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:我是 Asp.net 核心的新手,在尝试解决这个问题时遇到了一些麻烦:

I have 2 controllers: a "Course" controller and a "Professor" controller.我有 2 个控制器:“课程”controller 和“教授”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".当用户看到 Professor model 中列出的条目并点击 details 时,它(像往常一样)调用“Professor”controller 中称为“details”的操作。

the Details action, when called, displays all of the classes a professor teaches.调用 Details 操作时,会显示一位教授教授的所有课程。 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).这就是我卡住的地方:一旦用户点击课程,我需要在“课程”controller 中调用 function 来显示用户点击的课程名称(以及课程的其他相关信息 - 这样作为课程的部门)。 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?我怎么能go做这个呢?

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:在我的教授 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)我的“课程”中的索引操作 controller(我需要根据之前点击的特定 class 用户找出一种显示相关信息的方法)

   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.您需要做的就是在您的<a>标签中添加链接到课程所需的信息。 Assuming your course has as an Id property that is its primary key, you would do something like this.假设你的课程有一个 Id 属性作为它的主键,你会做这样的事情。

@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 .然后让 Details 方法(或者您可以根据需要命名页面和方法)接受该参数id

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

You can provide any parameters you need for a view/method using asp-route-{parameterName}您可以使用asp-route-{parameterName}提供视图/方法所需的任何参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在环境变量中正确存储连接字符串,以便通过生产ASP.Net Core MVC应用程序进行检索 - How to correctly store connection strings in environment variables for retrieval by production ASP.Net Core MVC applications ASP.NET Core web 应用程序中 ADO.NET 大数据检索的性能 - Performance with ADO.NET large data retrieval in ASP.NET Core web app 关于在 ASP.NET Core 和 Docker 中持久化数据的初学者问题 - Beginner question about persisting data in ASP.NET Core and Docker ASP.Net Mvc-View调用可能导致数据检索的函数是否可以接受? - ASP.Net Mvc - Is it acceptable for the View to call functions which may cause data retrieval? 使用 ASP.NET Core MVC 将带有文件的数据发布到 api (ASP.NET Core Web API) - Post data with files using ASP.NET Core MVC to api (ASP.NET Core Web API) 在ASP.Net Core MVC中读取JSON post数据 - Read JSON post data in ASP.Net Core MVC ASP.NET MVC System.Data.Entity.Core.EntityCommandExecutionException - ASP.NET MVC System.Data.Entity.Core.EntityCommandExecutionException 如何调试 ASP.NET Core MVC 数据绑定? - How to debug ASP.NET Core MVC data binding? 用数据填充弹出模式 ASP.NET CORE MVC Jquery - Fill Popup modal with data ASP.NET CORE MVC Jquery 使用ASP.NET MVC Core将数据从数据库注入到类中 - Inject data from database to a class with ASP.NET MVC Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM