Thursday 23 March 2017

MVC - Database First - Display 2 models in 1 view



Watch this example on YouTube

1. Tables
CREATE TABLE [dbo].[Product](
    [ProductID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
 CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
    [ProductID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


CREATE TABLE [dbo].[ProductHistory](
    [HistoryID] [int] IDENTITY(1,1) NOT NULL,
    [ProductID] [int] NULL,
    [PreviousName] [varchar](50) NULL,
    [NewName] [varchar](50) NULL,
 CONSTRAINT [PK_ProductHistory] PRIMARY KEY CLUSTERED
(
    [HistoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

2.SP
CREATE PROCEDURE [dbo].[LoadParticularProduct]
    @Id int
AS
BEGIN
    SET NOCOUNT ON;

    Select * From Product Where ProductID = @Id
END

GO


CREATE PROCEDURE [dbo].[LoadProductHistory]
    @ProductID int
AS
BEGIN
    SET NOCOUNT ON;

    Select * From ProductHistory Where ProductID = @ProductID Order By ProductID
END

GO



CREATE PROCEDURE [dbo].[LoadProducts]
AS
BEGIN
    SET NOCOUNT ON;

    Select * From Product
END

GO



CREATE PROCEDURE [dbo].[UpdateProduct]
    @ProductID int,
    @ProductName varchar(50)
AS
BEGIN
    SET NOCOUNT ON;

    Declare @PreviousProductName varchar(50) = (Select Name From Product Where ProductID = @ProductID)
    Update Product Set Name = @ProductName Where ProductID = @ProductID
    Insert Into ProductHistory (ProductID, PreviousName, NewName) Values
        (@ProductID, @PreviousProductName, @ProductName)
END

GO

3.New class in model
    public class ProductList
    {
        public LoadParticularProduct_Result prod { get; set; }
       public IEnumerable<LoadProductHistory_Result> hist { get; set; }
    }
4. Controller

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

namespace MVC2Model.Controllers
{
    public class ProductController : Controller
    {
        CompanyEntities db = new CompanyEntities();
        // GET: Product
        public ActionResult Index()
        {
            var res = from s in db.LoadProducts()
                      select s;
            return View(res);
        }

        public ActionResult Edit(int id)
        {
            ProductList pr = new ProductList();
            pr.prod = db.LoadParticularProduct(id).FirstOrDefault();
            if(pr.prod == null)
            {
                return HttpNotFound();
            }
            pr.hist = db.LoadProductHistory(id);
            return View(pr);
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection col)
        {
            if (ModelState.IsValid)
            {
                db.UpdateProduct(id, col["prod.Name"]);
            }
            return RedirectToAction("Index");
        }
    }
}

5. View Index
@model IEnumerable<MVC2Model.Models.LoadProducts_Result>
@{
    ViewBag.Title = "Index";
}

<table>
    <tr>
        <th>
            @Html.LabelFor(model=>model.First().Name)
        </th>
        <th></th>
    </tr>
    @foreach(var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem=>item.Name)
            </td>
            <td>
                @Html.ActionLink("Edit/View Product", "Edit", new { id=item.ProductID})
            </td>
        </tr>
    }
</table>

6. View - Edit

@model MVC2Model.Models.ProductList
@{
    ViewBag.Title = "Edit";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <table>
        <tr>
            <th>
                @Html.LabelFor(model=>model.prod.Name)
            </th>
            <th></th>
        </tr>
        <tr>
            <td>
                @Html.EditorFor(model=>model.prod.Name)
            </td>
            <td>
                <input type="submit" value="save" />
            </td>
        </tr>
    </table>
}
<table>
    <tr>
        <th>
            @Html.LabelFor(model=>model.hist.First().PreviousName)
        </th>
        <th>
            @Html.LabelFor(model => model.hist.First().NewName)
        </th>
    </tr>
    @foreach(var item in Model.hist)
    {
        <tr>
            <td>
                @Html.DisplayFor(model=>item.PreviousName)
            </td>
            <td>
                @Html.DisplayFor(model => item.NewName)
            </td>
        </tr>
    }
</table>

No comments:

Post a Comment