如何在ASP.NET MVC 5中的Grid中显示数据库表/数据

时间:2015-08-08 14:19:09

标签: asp.net-mvc-5 grid

我有2个表tblCompanytblProduct。 我想在数据库中添加细节,我成功完成了。 我希望通过@foreach在网格中显示此详细信息,但在将ListBox值GetCompanyList()和网格GetProductDetails()的数据从同一控制器显示到同一视图时会出现冲突。

我尝试了多种方式在网格中显示数据并通过may网站,所有网站都用单独的控制器和视图进行解释,但我希望它在单个控制器和视图中。我正在使用实体框架。

我会发布我的完整代码,以便你们可以帮助我。请参阅随附的屏幕截图以了解输出。

输出屏幕截图

Output Screenshot Image

ProductDBController.cs

using MVC.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC.Controllers
{
    public class ProductDBController : Controller
    {
        ProductDBEntities PDBE = new ProductDBEntities();

        //
        // GET: /ProductDB/Index ==> ProductDBView

        public ActionResult Index(ProductDBModel PDBM)
        {
            PDBM.CompanyNameList = GetCompanyList();
            PDBM.ProductDetails = GetProductDetails();

            return View("ProductDBView", PDBM);
        }

        public ActionResult AddCompanyName(tblCompany tC, string action)
        {
            if (action == "Add")
            {
                PDBE.tblCompanies.Add(tC);
                PDBE.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult AddProductDetails(tblProduct tP, string action)
        {
            if (action == "Add")
            {
                PDBE.tblProducts.Add(tP);
                PDBE.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public SelectList *GetProductDetails()
        {
            List<GetProductDetails_Vw> Vw = PDBE.GetProductDetails_Vw.ToList();
            return new SelectList(Vw);
        }

        public SelectList GetCompanyList()
        {
            tblCompany tC = new tblCompany();
            tC.CompanyID = 0;
            tC.CompanyName = "Select";

            List<tblCompany> li = PDBE.tblCompanies.ToList();
            li.Insert(0, tC);

            return new SelectList(li, "CompanyID", "CompanyName");
        }
    }
}

ProductDBModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC.Models
{
    public class ProductDBModel
    {
        public int CompanyID { get; set; }
        public string CompanyName { get; set; }
        public SelectList CompanyNameList { get; set; }
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
        public int ProductPrice { get; set; }
        public SelectList ProductDetails { get; set; }
        public IEnumerable<object> GetProductDetails_Vw { get; set; }
    }
}

ProductDBView.cshtml

@model MVC.Models.ProductDBModel
@{
    ViewBag.Title = "Product Dashboard";
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Product Dashboard</title>
</head>
<body>
    <div>
        <table style="width:100%">
            <tr>
                <td style="width:30%">
                    @using (Html.BeginForm("AddCompanyName", "ProductDB", FormMethod.Post))
                    {
                        <table>
                            <tr>
                                <td>
                                    Company Name
                                </td>
                                <td>
                                    :
                                </td>
                                <td>
                                    @Html.TextBoxFor(m => m.CompanyName)
                                </td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    <br />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    @*<input type="button" value="Add" onclick="location.href='@Url.Action("AddCompanyName", "ProductDB")'" />*@
                                    <input type="submit" value="Add" name="action" />
                                </td>
                                <td></td>
                                <td>
                                    <input type="button" value="Clear" name="action" />
                                </td>
                            </tr>
                        </table>
                    }
                </td>
                <td style="width:70%">
                    @using (Html.BeginForm("AddProductDetails", "ProductDB", FormMethod.Post))
                    {
                        <div>
                            <table>
                                <tr>
                                    <td>
                                        Company Name
                                    </td>
                                    <td>
                                        :
                                    </td>
                                    <td>
                                        @Html.DropDownListFor(m => m.CompanyID, Model.CompanyNameList)
                                    </td>
                                    <td>
                                        Product Name
                                    </td>
                                    <td>
                                        :
                                    </td>
                                    <td>
                                        @Html.TextBoxFor(m => m.ProductName)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Product Description
                                    </td>
                                    <td>
                                        :
                                    </td>
                                    <td>
                                        @Html.TextAreaFor(m => m.ProductDescription)
                                    </td>
                                    <td>
                                        Product Price
                                    </td>
                                    <td>
                                        :
                                    </td>
                                    <td>
                                        @Html.TextBoxFor(m => m.ProductPrice)
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="3">
                                        <br />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="submit" value="Add" name="action" />
                                    </td>
                                    <td></td>
                                    <td>
                                        <input type="button" value="Clear" name="action" />
                                    </td>
                                </tr>
                            </table>
                        </div>
                    }
                </td>
            </tr>
        </table>
    </div>
    <hr />
    <div>
        <table>
===========================================================================
            @foreach (var item in Model.ProductDetails)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => Model.ProductID)</td>
                    <td>@Html.DisplayFor(modelItem => Model.CompanyName)</td>
                    <td>@Html.DisplayFor(modelItem => Model.ProductName)</td>
                    <td>@Html.DisplayFor(modelItem => Model.ProductDescription)</td>
                    <td>@Html.DisplayFor(modelItem => Model.ProductPrice)</td>
                </tr>
            }
===========================================================================
        </table>
    </div>
</body>
</html>

0 个答案:

没有答案