码迷,mamicode.com
首页 > Web开发 > 详细

MVC添加动态视图的参考代码。重点是添加部分视图的使用方法,非常有用的代码!!!!!!!!!!!!!!

时间:2015-07-14 10:00:14      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

这是在model里的两个查询方法

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"})

 

MVC添加动态视图的参考代码。重点是添加部分视图的使用方法,非常有用的代码!!!!!!!!!!!!!!

标签:

原文地址:http://www.cnblogs.com/275147378abc/p/4644605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!