Asp.net MVC 学习笔记 (3) --Controller 与View 之间的数据传递
主要总结Controller如何获取URL参数,以及Controller与View之间的数据传递。
1.Controller 数据传递给View
(1)使用强类型View
先定义自定义的实体:
代码
namespace MvcApplication1
{
[Serializable]
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
}
namespace MvcApplication1
{
/// <summary>
/// 购买之星
/// </summary>
public class FamourcePeople
{
/// <summary>
/// 用户姓名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 用户编号
/// </summary>
public string Userid { get; set; }
/// <summary>
/// 图片
/// </summary>
public string img { get; set; }
}
}
namespace MvcApplication1
{
/// <summary>
/// 商品列表视图
/// </summary>
[Serializable]
public class ProductIndex2
{
/// <summary>
/// 热门商品列表
/// </summary>
public List<Product> Hotlist
{
get;
set;
}
/// <summary>
/// 用户排行
/// </summary>
public List<User> UserList
{
get;
set;
}
/// <summary>
/// 购买之星
/// </summary>
public FamourcePeople fp { get; set; }
}
}
namespace MvcApplication1
{
[Serializable]
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
}
一个假的BLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.BLL
{
public class ProductBLL
{
public static List<Product> ProductList()
{
List<Product> list = new List<Product>();
for (int i = 1; i < 10; i++)
{
list.Add(new Product { Id = i + 1, Name = "Product_" + DateTime.Now.ToString() });
}
return list;
}
public static List<Product> ProductList(int Page)
{
List<Product> list = new List<Product>();
for (int i = (Page-1)*10+1; i < 10 * Page; i++)
{
list.Add(new Product { Id = i + 1, Name = "Product_" + DateTime.Now.ToString() });
}
return list;
}
public static List<User> UserList()
{
List<User> list = new List<User>();
for (int i = 0; i < 8; i++)
{
list.Add(new User { Id = i + 1, Name = "User_" + DateTime.Now.ToString() });
}
return list;
}
/// <summary>
/// 商品列表
/// </summary>
/// <returns></returns>
public static List<ProductIndex2> ProductIndex2List()
{
List<ProductIndex2> list = new List<ProductIndex2>();
list.Add(new ProductIndex2 { Hotlist = ProductBLL.ProductList(), UserList = ProductBLL.UserList(), fp = new FamourcePeople { img = "no.gif", Userid = "1", UserName = "admin" } });
return list;
}
public static bool Create(Product model)
{
return true;
}
public static bool Edit(Product model)
{
return true;
}
public static bool Del(int id)
{
return true;
}
}
}
ProductController 中IndexAction的实现:
public ActionResult Index()
{
string Method = Request.HttpMethod;
List<Product> list = new List<Product>();
list = ProductBLL.ProductList();
return View(list);
}
View展示数据:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Product>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<table>
<tr>
<th></th>
<th>
Id
</th>
<th>
Name
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%> |
<%= Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= Html.Encode(item.Id) %>
</td>
<td>
<%= Html.Encode(item.Name) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
</asp:Content>
(2)弱类型View
有 ViewData 与 ViewData 区别就不介绍了。当然你用弱类型View想用智能提示可以通过强制转换实现,如下:
Controllers
public ActionResult Index1()
{
List<Product> list = new List<Product>();
list = ProductBLL.ProductList();
ViewData["list"] = list;
return View();
}
View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index1
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index1</h2>
<table>
<tr>
<th></th>
<th>
Id
</th>
<th>
Name
</th>
</tr>
<% foreach (var item in ViewData["list"] as List<MvcApplication1.Product>) { %>
<tr>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%> |
<%= Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= item.Id %>
</td>
<td>
<%= item.Name %>
</td>
</tr>
</tr>
<% } %>
</table>
</asp:Content>
如果要展示的View较为复杂,你可以定义一个对应的实体,比如这个例子:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.ProductIndex2>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index2
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Index2</h2>
<table>
<% foreach (var item in Model)
{ %>
<% foreach (var obj in item.Hotlist)
{%>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>
|
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
|
<%= Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= obj.Id%>
</td>
<td>
<%= obj.Name%>
</td>
</tr>
<% } %>
<% foreach (var obj in item.UserList)
{%>
<tr>
<td>
<%=Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>
|
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
|
<%= Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= obj.Id%>
</td>
<td>
<%= obj.Name%>
</td>
</tr>
<% } %>
<tr>
<% =item.fp.UserName %>
</tr>
<% } %>
</table>
</asp:Content>
2. View 向Controller传递数据
一般三种方法:
(1)
string username = Request.Form["UserName"];
(2)
public ActionResult ActionName(FormCollection formCollection)
{
string username = formCollection["UserName"];
string password = formCollection["Password"];
return View();
}
(3)通过设置View为强类型视图
public ActionResult Create(Product model)
{
string aa = Request.HttpMethod;
if (model.Id > 50)
{
ModelState.AddModelError("Id", "ID不能大于50");
}
if (model.Name.Length > 3)
{
ModelState.AddModelError("Name", "名字不得超过3个字符");
}
if (ModelState.IsValid)
{
ProductBLL.Create(model);
return RedirectToAction("Index");
}
else
{
return View(model);
}
}
3.Controller 如何接收Url中的参数
public ActionResult Index(int id)
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
如果id 允许为空,可如下定义
public ActionResult Index(int? id)
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
对于分页,可以使用强大的MvcPager
推荐文章:http://www.cnblogs.com/cdts_change/archive/2010/09/21/1832874.html


浙公网安备 33010602011771号