使用jQuery的JSON数据的ASP MVC弹出窗口

时间:2015-10-04 07:46:21

标签: jquery json asp.net-mvc

我是ASP MVC的新手。虽然我想从按钮单击创建一个弹出窗口,这个弹出窗口需要通过从SQL数据库获取与按钮ID相关的数据来显示数据列表。到目前为止,我得到了JSON响应但该结果如何进入弹出窗口?

请帮我解决这个问题。这是我到目前为止的代码

控制器:

[HttpPost]
public JsonResult StudentCourseList(int id) 
{
    try
    {
        StudentBusinessLayer studentBusinessLayer = new StudentBusinessLayer();
        StudentCourseVM studentcourse = studentBusinessLayer.AllStudent.Single(stu => stu.StudentId == id);
        List<Course> coursestudentList = studentBusinessLayer.studentCourseList(id).ToList();
        studentcourse.selectcourseList = coursestudentList;
        return Json(studentcourse, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        throw new Exception("Exception in Course detials");
    }
}

public ActionResult Index()
{
    if (ModelState.IsValid)
    {
        StudentCourseVM courseStudent = new StudentCourseVM();
        StudentBusinessLayer studentBusinessLayer = new StudentBusinessLayer();
        List<StudentCourseVM> students = studentBusinessLayer.AllStudent.ToList();
        return View(students);                         
    }
    return View();
}

索引页:

@model IEnumerable<BusinessLayer.StudentCourseVM>
....
<head>
    <script src="~/Scripts/jquery-2.1.4.js"></script>
    <script src="~/Scripts/bootstrap.js"></script>
    <script src="~/Scripts/jquery-ui-1.11.4.js"></script>
</head>
<p>@Html.ActionLink("Create New", "Create")</p>
<table>
    <tr>
        <th>@Html.DisplayNameFor(model => model.StudentId)</th>
        <th>@Html.DisplayNameFor(model => model.StudentName)</th>
        <th>@Html.DisplayNameFor(model => model.StudentAddress)</th>
        <th>@Html.DisplayNameFor(model => model.CourseName)</th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.StudentId)</td>
            <td>@Html.DisplayFor(modelItem => item.StudentName)</td>
            <td>@Html.DisplayFor(modelItem => item.StudentAddress)</td>
            <td>
                <button type="button" class="coursesList" id ="@Html.DisplayFor(modelItem => item.StudentId)" onclick="LoadData(@Html.DisplayFor(modelKItem => item.StudentId))">Course Lists</button>
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.StudentId }) |
                @Html.ActionLink("Details", "Details", new { id = item.StudentId }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.StudentId })
            </td>
        </tr>
    }
</table>

<div id="coursePopup" style="display: none" >
    <div>
        <table class="table table-striped table-hover">
            <tr>
                <td>Course List</td>
            </tr>
           <tr></tr> 
        </table>
    </div>
</div>

<script type="text/javascript">
    function LoadData(id) {
        $.ajax({            
            data: {},
            modal: true,
            datatype: "JSON",
            type: "POST",
            url: "/Student/StudentCourseList?id="+id,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                console.log(JSON.stringify(data));
            },
            error: function (data) {
                alert("Error in process");
            }
        });
    }
</script>

型号:

public List<Course> studentCourseList(int id)
{    
    string connectionString = ConfigurationManager.ConnectionStrings["Student"].ConnectionString;
    List<Course> courseList = new List<Course>();
    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        SqlCommand sqlCommand = new SqlCommand("StudentCourseList", sqlConnection);
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlConnection.Open();
        SqlParameter sqlPrm = new SqlParameter();
        sqlPrm.ParameterName = "@StudentId";
        sqlPrm.Value = id;
        sqlCommand.Parameters.Add(sqlPrm);
        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
        Student student = new Student();                    
        while (sqlDataReader.Read())
        {
            Course course = new Course();
            course.CourseName = sqlDataReader["CourseName"].ToString();
            courseList.Add(course);
        }
    }
    return courseList; 
}

0 个答案:

没有答案