• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.netMVC中的ViewData与ViewBag

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在Asp.net MVC 3 web应用程序中,我们会用到ViewDataViewBag,对比一下: 

 

ViewData ViewBag
它是Key/Value字典集合 它是dynamic类型对像
从Asp.net MVC 1 就有了 ASP.NET MVC3 才有
基于Asp.net 3.5 framework 基于Asp.net 4.0与.net framework
ViewData比ViewBag快 ViewBag比ViewData慢
在ViewPage中查询数据时需要转换合适的类型 在ViewPage中查询数据时不需要类型转换
有一些类型转换代码 可读性更好

 


在Controller中使用ViewData:

public ActionResult UsingViewData()
{
    
    ViewData["Title"] = " Using ViewData";
    ViewData["ProjectName"] = "My Test Project";
    ViewData["ProjectDescription"] = "This is Test Project to demo Viewdata and viewbag details";
    ViewData["StartDate"] = new DateTime(2011, 1, 1);
    ViewData["TotalPrice"] = 1000;
    ViewData["TotalDays"] = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client","Mr.  Client");
    stackholder.Add("Manager","Mr. Joy");
    stackholder.Add("Team Leader","Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewData["stackholder"] = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewData["modules"] = modules;
    return View();
}

 

对应UsingViewData View的cshtml:

<h1>@ViewData["Title"]</h1>
 <div>
   <div>
    <h2>Project Name : @ViewData["ProjectName"]</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewData["ProjectDescription"]".</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewData["stackholder"] as Dictionary<string, string> )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewData["modules"] as List<string> )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewData["StartDate"]  <br />
     Project TotalPrice: @ViewData["TotalPrice"] <br />
     Project TotaDays  : @ViewData["TotalDays"] 
   </div>
 </div>

 

然后是ViewBag:

public ActionResult UsingViewBag()
{

    ViewBag.Title = " Using ViewBag";
    ViewBag.ProjectName = "My Test Project";
    ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";
    ViewBag.StartDate = new DateTime(2011, 1, 1);
    ViewBag.TotalPrice = 1000;
    ViewBag.TotalDays = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewBag.stackholder = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewBag.modules = modules;
    return View();
}

 


对应View UsingViewBag 的cshtml的ViewBag:

<h1>@ViewBag.Title</h1>
 <div>
   <div>
    <h2>Project Name : @ViewBag.ProjectName</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewBag.ProjectDescription.</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewBag.stackholder )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewBag.modules )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") <br />
     Project TotalPrice: @ViewBag.TotalPrice  ₹  <br />
     Project TotaDays  : @ViewBag.TotalDays 
   </div>
 </div>

 


后面是在Controller中使用ViewBag:

public ActionResult UsingViewBagInControlAndViewDataInView()
{

    ViewBag.Title = " Using ViewBag In Control And ViewData In View";
    ViewBag.ProjectName = "My Test Project";
    ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";
    ViewBag.StartDate = new DateTime(2011, 1, 1);
    ViewBag.TotalPrice = 1000;
    ViewBag.TotalDays = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewBag.stackholder = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewBag.modules = modules;
    return View();
}

 


在UsingViewBagInControlAndViewDataInView.cshtml中使用ViewData来取值:

<h1>@ViewData["Title"]</h1>
 <div>
   <div>
    <h2>Project Name : @ViewData["ProjectName"]</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewData["ProjectDescription"]".</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewData["stackholder"] as Dictionary<string, string> )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewData["modules"] as List<string> )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewData["StartDate"]  <br />
     Project TotalPrice: @ViewData["TotalPrice"] <br />
     Project TotaDays  : @ViewData["TotalDays"] 
   </div>
 </div>

 


反过来是这样的: 

public ActionResult UsingViewDataInControlAndViewBagInView()
{
    ViewData["Title"] = " Using ViewData In Control And ViewBag In View";
    ViewData["ProjectName"] = "My Test Project";
    ViewData["ProjectDescription"] = "This is Test Project to demo Viewdata and viewbag details";
    ViewData["StartDate"] = new DateTime(2011, 1, 1);
    ViewData["TotalPrice"] = 1000;
    ViewData["TotalDays"] = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewData["stackholder"] = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewData["modules"] = modules;
    return View();
}

 


在UsingViewDataInControlAndViewBagInView.cshtml 的View中使用ViewBag来取值:

<h1>@ViewBag.Title</h1>
 <div>
   <div>
    <h2>Project Name : @ViewBag.ProjectName</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewBag.ProjectDescription.</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewBag.stackholder )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewBag.modules )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") <br />
     Project TotalPrice: @ViewBag.TotalPrice  ₹  <br />
     Project TotaDays  : @ViewBag.TotalDays 
   </div>
 </div>

 


这样对比看上去会比较清楚一些。在源代码中的ViewBag是这样的属性:

public dynamic ViewBag {
    get {
        if (_dynamicViewData == null) {
            _dynamicViewData = new DynamicViewDataDictionary(() => ViewData);
        }
        return _dynamicViewData;
    }
}

 

ViewData是:

public ViewDataDictionary ViewData {
    get {
        if (_viewData == null) {
            SetViewData(new ViewDataDictionary());
        }
        return _viewData;
    }
    set {
        SetViewData(value);
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.net实现同页面内多图片自动上传并带预览显示发布时间:2022-07-10
下一篇:
【记录】ASP.NET URL 特殊字符发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap