MVC页面传值总结

引言

       我们都在使用mvc这个前端框架,让我们的U层再次解耦,今天小编就来总结一下MVC中的页面传值。我们分为Controller----------àViiew和View-----------àController传值

一、Controller----------àViiew

       1、  ViewData

      使用ViewData是采用键值对的形式,对所定义的数据进行传递。在View中会自动识别到拥有唯一键值对的ViewData,并将数据显示出来。

 

     例子:

Public ActionResult()
{
   List list = (from d in db.BlogArticles where d.AIsDel == false select d).ToList();
//利用Viewdata形式
ViewData[“DataList”]=list;
retrun View();
}

      视图中的接收



        @foreach (BlogArticle a in ViewData["DataList"] as List)
        {
            
        }
@a.AId @a.ATitle @a.ACate @a.AStatu @a.AUpdatetime @a.AContent

      2、  ViewBag

       获取视图包,允许自定义属性进行赋值,属于动态类型,以ViewBag.属性 = 属性值的方式进行传值。


       例子:

public ActionResult Index()
{
//以ViewBagel方式传值
         ViewBag.Title="Hello!";
}

       视图中接收

ViewBag.Title


二、从View---------àController

        通常我们会利用ajax来通过get或者post进行提交。就是$.ajax({type,url,data,success:function(){})


总结

        使用什么高端的框架我们都要善于总结,总结了才是自己的,加油!

你可能感兴趣的:(MVC页面传值总结)