• MVC 4.0 Linq to SQL Model


    下面介绍了如何从SQL 2008R2 中的数据库获取数据,并将数据通过View呈现出来:

    Step 1:

      创建一个MVC4.0 的Internet App,选择 Razor作为View engine.

    Step 2:

      在创建好的Project的Models文件夹上 右键-〉Add-> LINQ to SQL Classes ->命名(StudentCourseSelection)

    Step 3:

      在VS的Server Explorer上连接自己的数据库,展开数据库中的Tables文件夹,将需要的表 都拖到上一步新建的LINQ to SQL class中,如:

    保存此文件

    Step 4:

      察看一下上一步中VS为我们自动生成的代码,会发现 已经有这样的一个类了:StudentCourseSelectionDataContext。

      接下来我们就可以使用此DbContext。

      打开位于Controlls文件夹下的HomeController.cs文件,将代码改成:

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MVCDemo.Models;
    namespace MVCDemo.Controllers
    {
        public class HomeController : Controller
        {
            //注意自己的Connectionstring
            private MVCDemo.Models.StudentCourseSelectionDataContext DbContext = new StudentCourseSelectionDataContext(@"Data Source=VS2012\ZACKZHOU;Initial Catalog=student-course;Integrated Security=True");
            
            public ActionResult Index()
            {
                ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
    
                return View();
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "Your app description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
    
            public ActionResult Students() 
            {
                System.Linq.IQueryable<Student> students = from stu in DbContext.Students
                                                           select stu;
                return View(students);
            }
        }
    }


    Step 5 :

      右键代码中的 Students() -〉 Add View

      

    在这个例子中,Model Class应该是:System.Linq.IQueryable<MVCDemo.Models.Student>。

    Step 6 :

      在Views文件夹下面会生成一个Students.cshtml 文件,打开此文件并作如下修改:

    @model System.Linq.IQueryable<MVCDemo.Models.Student>
    
    @{
        ViewBag.Title = "Students";
    }
    
    <h2>Students</h2>
    
    <ul>
        @foreach(MVCDemo.Models.Student stu in ViewData.Model){
            <li>
                @stu.Name : @stu.Age
            </li>
      }
    </ul>

      注意: @model所引用的类型 必须和Controll中返回的一致,否则会出现一些奇怪的错误,如:The model item passed into the dictionary is of type     'System.Data.Linq.DataQuer' 。。。。


    Step 7:

      在Views -> Shared 下的 _Layout.cshtml 的代码中添加一行,如下:

      

                            <ul id="menu">
                                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                                <li>@Html.ActionLink("About", "About", "Home")</li>
                                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                                <li>@Html.ActionLink("Students","Students","Home")</li>
                            </ul>


    Step 8:

      运行此程序,出现这样的结果:

    Over: 希望对一些新学 MVC的朋友有些帮助。

  • 相关阅读:
    greenplum表的distributed key值查看
    oracle dump的使用心得
    Linux du与df命令的差异
    从语言只是工具说起
    DDD领域模型
    同一个对象在另一个对象中容易出现重复引用造成map覆盖,HiJson出现严重漏洞自动删除了$ref和空值
    乒乓球相关
    字符串转xml
    最新版java题
    list集合进行分页
  • 原文地址:https://www.cnblogs.com/FsharpZack/p/2807049.html
Copyright © 2020-2023  润新知