Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c-如何在MVC中显示同一网页内的查询结果_C#_Razor_Model View Controller_Dropdown - Fatal编程技术网

C# c-如何在MVC中显示同一网页内的查询结果

C# c-如何在MVC中显示同一网页内的查询结果,c#,razor,model-view-controller,dropdown,C#,Razor,Model View Controller,Dropdown,我有一个MVC应用程序,在我从下拉菜单中选择值后,执行处理sql的函数。下拉列表位于: http://localhost:9030/Courses/Index 在我做出选择后,结果是: http://localhost:9030/Courses/getCoursesByTeachers?Teacher=(here is an ID of the element) getCoursesByTeachers是控制器中的函数,它将ID作为参数 以下是函数: public string getCou

我有一个MVC应用程序,在我从下拉菜单中选择值后,执行处理sql的函数。下拉列表位于:

http://localhost:9030/Courses/Index
在我做出选择后,结果是:

http://localhost:9030/Courses/getCoursesByTeachers?Teacher=(here is an ID of the element)
getCoursesByTeachers是控制器中的函数,它将ID作为参数

以下是函数:

public string getCoursesByTeachers(int Teacher)
        {
            string sql = "SELECT * FROM Course WHERE teacher_Id = @Teacher";
            string connectionString = "Data Source=TSSKKEWKS0619;Initial Catalog=T-Timetable;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";
            //string course = "No courses are being taught by this teacher";
            List<string> list = new List<string>();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(sql, connection);
                command.Parameters.AddWithValue("Teacher", Teacher);
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        list.Add(reader["name"].ToString());
                    }
                }
                finally
                {
                    reader.Close();
                }
            }
            if (list.Count==0)
            {
                list.Add("There are no courses being taught by this teacher.");
            }

            return string.Join(System.Environment.NewLine, list);
        } 
以下是我在cshtml中如何称呼它:

<h2>Courses taught by:</h2>

    @using (Html.BeginForm("getCoursesByTeachers", "Courses", FormMethod.Get))
    {
        @Html.DropDownList("Teacher", (SelectList)ViewBag.Teacher, "Select teacher", new { onchange = @"form.submit()" }); // form.action('getCoursesByTeacher')
    }
有没有办法修改它,使结果不会显示在重定向页面上,而是显示在url中的相同页面中:。也许在它下面,我猜我必须在cshtml中以某种方式更改它,对吗?我不知道怎么做,也不想在它工作时把它搞砸:D所以我想我问你们


希望问题足够清楚。感谢您的帮助。

使用AJAX检索结果,然后将其插入当前DOM。如果标签仅用于发送下拉数据,则可以删除该标签。 我在下面的示例中使用jQuery

<div id="teacherResultDisplay">@*result will be inserted here*@</div>

// load on dropdown change
$('#Teacher').change(function () {
    var selected = $(this).val(); // assume the <option value="..."/> is the teacher id

    $.ajax({
        type: 'GET',
        cache: false, /* to prevent problems with dynamic data that gets cached */
        url: '@Url.Action("getCoursesByTeachers", "Courses")',
        data: {
            Teacher: selected
        },
        success: function (stringResult) {
            $('#teacherResultDisplay').html(stringResult);
        }
    });
});

您的函数是否需要由教师调用getCoursesByTeachers而不是index

如果您无法更改窗体操作的位置。您可以将getCoursesByTeachers重命名为index,并具有以下内容

public string getCoursesByTeachers(int Teacher)
{
     return RedirectToAction("index", new { Teacher= Teacher });
}
您可以使用HttpPost属性,使用相同的索引操作方法并从中调用getCoursesByTeachers方法返回相同的视图,也可以使用接收AJAX调用并在同一页面上呈现查询结果的部分视图注意,以这种方式,getCoursesByTeachers必须转换为返回结果部分视图的ActionResult。