zoukankan      html  css  js  c++  java
  • MVC第二天

    昨天研究了一下空的MVC网站包含的一些网页,其中是MVC基本用法的例子,还是很值得学习和借鉴的。
    最后总结出来一个问题:
    Controller把数据交给View用ViewData,而View如果要传数据给Controller的话要通过参数的方法。
    比如说经典的登录页面,登录要提交两个数据:loginId, loginpwd,那么在表单中就必须要有这两个表单元素,
    可以给这两个表单元素指定个name属性,那么在表单提交的时候数据就会被提交到Contoller里面相对应的方法中表单数据将会做为方法的参数,
    这些提交的数据包含:Form中的数据,url参数,其中应该也可以包含cookie(以后再研究)。
    在这个基础上想一个问题,比如说数据库有个表,现在做一个页面,上面提供个下拉列表,选择一项后提交数据,其后到另一个页面中将这个下拉列表中的相关数据查询出来。并显示在ListView或Repeater中。
    思路有了,下面就开始动手。
    先建一个SearchController:

        
    public class SearchController : Controller
        {
            
    //
            
    // GET: /Search/
            BookDataClassesDataContext bookDB = new BookDataClassesDataContext();

            
    public ActionResult Index()
            {
                var categories 
    = (from category in bookDB.Categories
                                 select 
    new SelectListItem
                                 {
                                     Value 
    = category.Id.ToString(),
                                     Text 
    = category.Name
                                 }).ToList();
                SelectListItem item 
    = new SelectListItem();
                item.Text 
    = "请选择";
                item.Value 
    = "0";
                categories.Insert(
    0,item);
                 
                ViewData[
    "Categories"= categories;
                var publishers 
    = (from publisher in bookDB.Publishers
                                 select 
    new SelectListItem
                                 {
                                     Value 
    = publisher.Id.ToString(),
                                     Text 
    = publisher.Name
                                 }).ToList();
                publishers.Insert(
    0,item);
                ViewData[
    "Publishers"= publishers;

                
    return View();
            }
            
    public ActionResult Book(int catagory) {

                
    if (catagory != 0) {
                    var list 
    = from book in bookDB.Books
                               
    where book.Categories.Id == catagory 
                               select book;
                    
    this.ViewData["Books"= list;
                    
    return this.View();
                }
                
    return null;
            }
        }

    Index View代码如下
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>Index</title>
    </head>
    <body>
        
    <div>
         
    <% using (Html.BeginForm("Book""Search")) { %>
           
    <label for="catagory">分类:</label>
            
    <%= Html.DropDownList("catagory", (IEnumerable<SelectListItem>)ViewData["Categories"])%>     
            
    <br /> 
            
    <input type="submit" value="查找" />   
         
    <%%>
        
        
    </div>
    </body>
    </html>

    Book视图代码如下:
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

        protected 
    void Page_Load(object sender, EventArgs e) {
            
    this.lvBook.DataSource = this.ViewData["Books"];
            
    this.lvBook.DataBind();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>Book</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
           
    <asp:DataList runat="server" ID="dlBooks">
           
    <ItemTemplate>
           
    </ItemTemplate>
           
    </asp:DataList>
        
    </div>
        

        
    <asp:ListView ID="lvBook" runat="server" ItemPlaceholderID="itemPlaceholder">
        
    <ItemSeparatorTemplate >
        
    </ItemSeparatorTemplate>
        
    <ItemTemplate>      
    <tr><td><%Eval("CategoryId")%>:<%Eval("Title")%></td></tr>
        
    </ItemTemplate>
        
    <LayoutTemplate>
       
       
    <table><tr ID ="itemPlaceholder" runat="server"></tr></table>
        
    </LayoutTemplate>
        
    </asp:ListView>
        
    </form>
    </body>
    </html>
    既然这个操作没有问题那么对于数据库的其它操作用同样的道理就可以解决了。
  • 相关阅读:
    MongoDB Schema Design
    WinDBG中的poi是做什么用的?
    如何在Visual Studio中运行和调试汇编代码?
    [翻译图书] 未完工 Moving Applications to the Cloud on the Microsoft Windows Azure Platform 4
    在Word中生成随机的样本文本
    Quiz Win32内存表示与数值大小
    rep stos dword ptr es:[edi] 是做什么的?
    Windows Azure中虚拟机无法启动, 报错RoleStateUnknown的解决方案
    COM基础介绍
    64位的dump里如何寻找第一个到第四个参数?
  • 原文地址:https://www.cnblogs.com/hawkon/p/1559138.html
Copyright © 2011-2022 走看看