MVCは、ダイナミックビューの参照コードを追加する.ポイントは、一部のビューの使い方を追加することで、非常に有用なコード!!!

16711 ワード

これはモデル内の2つのクエリー方法です
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



namespace Mvcyemian.Models

{

    public class NewsBF

    {

        private mydboDataContext _Context = new mydboDataContext();

        // type 

        public List<News> Select(string type)

        {

              var query = _Context.News.Where(p => p.type == type);

              if(query.Count()>0)

              {

                  return query.ToList();

              }

            return null;

        }

        // Id 

        public News Selectbykey(int id)

        {

            var query = _Context.News.Where(p => p.ids==id);

            if (query.Count() > 0)

            {

                return query.First();

            }

            return null;

        }

    }

}

 
これはコントローラのコードです
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using Mvcyemian.Models;



namespace Mvcyemian.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/



        public ActionResult Index()

        {

            return View();

        }

        public ActionResult ShowGN()

        {

            return View();

        }

        public ActionResult ShowGJ()

        {

            return View();

        }

        public ActionResult ShowYL()

        {

            return View();

        }

        public ActionResult ShowCJ()

        {

            return View();

        }

        public ActionResult ShowIndexPartialView(string newsType)

        {

            ViewBag.Data = new NewsBF().Select(newsType);

            return PartialView();

        }

        public ActionResult ShowDetils(int id)

        {

            News data = new NewsBF().Selectbykey(id);

            return View(data);

        }

    }

}

メインビュー内のコード
@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>MasterPage</title>

</head>

<body>

    <table width="100%" border="0" cellpadding="0" cellspacing="0">

        <tr>

            <td height="100px" bgcolor="blue"> </td>

        </tr>

        <tr>

            <td>

                <table width="100%" border="0" cellpadding="0" cellspacing="0">

                    <tr>

                        <td width="240" bgcolor="pink" align="center"> @*  *@                           @Html.Partial("~/Views/NewsPartial1.cshtml")

                        </td>

                        <td >@RenderBody()</td>

                        

                    </tr>

                </table>

            </td>

        </tr>

        <tr>

            <td algin="center" height="100px" bgcolor="blue"> <br/>2007~2015</td>

        </tr>

    </table>

</body>

</html>

部分ビューのコード
 <h3> </h3>

        <div>

             @Html.ActionLink(" ","ShowGN","Home")

             @Html.ActionLink(" ","ShowGJ","Home")

             @Html.ActionLink(" ","ShowYl","Home")

             @Html.ActionLink(" ","ShowCJ","Home")

        </div>                    

部分ビューのbodyコード
@using  Mvcyemian.Models;

@{

    List<News> list=ViewBag.Data as List<News>;

        }

<ul>

    @foreach (News data in list)

    { 

        <li>@Html.ActionLink(data.title, "ShowDetils", "Home", new { id=data.ids}, null)</li>

    }

</ul>

部分ビューのコード
@using Mvcyemian.Models;

@model News

@{

    ViewBag.Title = "ShowDetils";

    Layout = "~/Views/MasterPage.cshtml";

}

@{if (Model!=null)

{

    <h2>@Model.title</h2>

<div>

    @Model.memo

</div>



}

else

{

    <div> </div>

}

}

部分ビューのコード
@{

    ViewBag.Title = "ShowYL";

    Layout = "~/Views/MasterPage.cshtml";

}



<h2>ShowYL</h2>

@Html.Action("ShowIndexPartialView", new { newsType="3"})

  







@{

    ViewBag.Title = "ShowGN";

    Layout = "~/Views/MasterPage.cshtml";

}



<h2>ShowGN</h2>

@Html.Action("ShowIndexPartialView", new { newsType="2"})







@{

    ViewBag.Title = "ShowGJ";

    Layout = "~/Views/MasterPage.cshtml";

}



<h2>ShowGJ</h2>

@Html.Action("ShowIndexPartialView", new { newsType="1"})













@{

    ViewBag.Title = "ShowCJ";

    Layout = "~/Views/MasterPage.cshtml";

}



<h2>ShowCJ</h2>

@Html.Action("ShowIndexPartialView", new { newsType="0"})