在视图ASP.NET

时间:2017-10-30 09:03:55

标签: c# asp.net json asp.net-mvc view

我正在编写电影租赁计划。 我想在浏览器中查看当前客户租用的电影列表。 我添加了一个按钮"显示租借的电影"每个实体,以及#34; Show"成功取回阵列中的所有电影(参见屏幕截图) 现在我想显示行表中数组中的所有FilmNames,就像文本一样。 我该怎么办? 谢谢 ! here is the debuge screenshot here is a screenshot

这里是CustomersContoller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MoviePro.Models;
using System.Data.Entity;

namespace MoviePro.Controllers
{
    public class CustomersController : Controller
    {
        // GET: Customers
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult loaddata()
        {
            MyDatabaseEntities3 dc = new MyDatabaseEntities3();

            var customers = dc.Customers.Select(c => new
            {
                c.CustomerName,
                c.Phone,
                c.CustomerId,
            });
            return Json(new { data = customers }, JsonRequestBehavior.AllowGet);

        }

        public ActionResult ShowFilemsByCust(int id)
        {
            MyDatabaseEntities3 dc = new MyDatabaseEntities3();
            var query = (from f in dc.Films
                         join cf in dc.CustomersFilms on f.FilmId equals cf.FilmId
                         where cf.CustomerId == id
                         select new
                         {
                             filmName = f.FilmName
                         }).ToList();
            return Json(new { data = query }, JsonRequestBehavior.AllowGet);
        }

这是Customers.cshtml

    @{
    ViewBag.Title = "Index";
}
<a class="btn btn-primary" style="margin-bottom:10px" onclick="PopupForm('@Url.Action("AddOrEdit","Customers")')"><i class="fa fa-plus"></i> New Customer</a>

<h2>Customers</h2>

<div style="width:90%; margin:0 auto;">
    <table id="myTable">
        <thead>
            <tr>
                <th>Customer Name</th>
                <th>Phone</th>
                <th></th>
            </tr>
        </thead>
    </table>
</div>
<style>
    tr.even {
        background-color: #F5F5F5;
    }
</style>


@* Load datatable css *@
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
@* Load datatable js *@
@section Scripts{
    <script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script>

    <script>
        var Popup, dataTable;
        $(document).ready(function () {
            dataTable= $('#myTable').DataTable({
                "ajax": {
                    "url": "/Customers/loaddata",
                    "type": "GET",
                    "datatype": "json"
                },
                "columns" : [
                       { "data": "CustomerName", "autoWidth": true },
                        { "data": "Phone", "autoWidth": true },

                        {"data":"CustomerId" , "render" : function (data) {
                            return "<a class='btn btn-default btn-sm' onclick=PopupForm('@Url.Action("AddOrEdit","Customers")/" + data + "')><i class='fa fa-pencil'></i> Edit</a><a class='btn btn-danger btn-sm' style='margin-left:2px' onclick=Delete(" + data + ")><i class='fa fa-trash'></i> Delete</a><a class='btn btn-default btn-sm' style='margin-left:2px'  onclick=Show(" + data + ")>Show Rented Movies</a>";
                        },
                            "orderable": false,
                            "searchable": false,
                            "width": "150px"
                        }
                ],
                "language": {

                    "emptyTable": "No data found, Please click on <b>Add New</b> Button"
                }
            });
        });


        function Show(id) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("ShowFilemsByCust", "Customers")/' + id,
                    success: function (data) {
                        /*
                        if (data.success) {
                            dataTable.ajax.reload();
                            $.notify(data.message, {
                                globalPosition: "top center",
                                className: "success"
                            })
                        }*/
                    }
                });
        }

    </script>
}

1 个答案:

答案 0 :(得分:0)

我认为你应该去一个customerMovie页面,而不是多用途的同一个网格。