您的位置:首页 > 其它

MVC传值——Controller向View传值(一)

2016-04-09 21:23 323 查看
    这几天一直在鼓捣传值的这个东西,之前只知道个ViewData,ViewBag,其他的就不会了。在之前学习ViewData和ViewBag的时候,只传了简单的一个字符串。可是现在,我们要传递的数据不仅仅是字符串啊,可能是一个List对象,也可能是Json串。于是做了一些小例子,总结一下传值的经验。

    从Controller向View传值的主要有4种:ViewData,ViewBag,TempData,Model。当然我们还可以添加JS代码进行传值。

    一、使用ViewData进行传值

     ViewData就是一个字典,它存放的是键值对。只对当前的View有效.

    1、传递字符串

    controller代码: 

public ActionResult Index()
{
ViewData["MessageOne"] ="Hello Cindy,I send the message to you by using the ViewData!";
return View();
}


    
    页面代码:

<h3>@ViewData["MessageOne"]</h3>


     

    2、传递list对象

     controller代码:
 
public ActionResult Index()
{
var person =new List<string>
{
"Cindy",
"23",
"Girl"
};
ViewData["Person"] =person;
}


     
    页面代码:

<h3>Hello,Cindy!Isend you a list by using the ViewData:</h3>
<ul>
@foreach (var person in(List<string>) ViewData["Person"])
{
<li>
@person
</li>
}
</ul>


   二、使用ViewBag进行传值

   
ViewBag存放的不是键值对,是dynamic动态类型,它不需要转型就可以使用里面的值。

    1、传递字符串

     controller代码: 

public ActionResult Index()
{
ViewBag.MessageOne = "Hello Cindy,I send the message to you by usingthe ViewBag!";
return View();
}


    页面代码:

<h3>@ViewBag.MessageOne</h3>


    2、传递list对象

     controller代码:

public ActionResult Index()
{
var person =new List<string>
{
"Cindy",
"23",
"Girl"
};
ViewBag.Person = person;
}


     页面代码:
 
<h3>Hello,Cindy!Isend you a list by using the ViewBag:</h3>
<ul>
@foreach (var person in  ViewBag.Person)
{
<li>
@person
</li>
}
</ul>


    三、使用TempData进行传值

 
     TempData其实和ViewData的用法类似,但是TempData可以用于不同View之间的传值,而不向ViewData一样只对当前的View有效。

     controller代码:

public ActionResult Index()
{
this.TempData["MyName"] ="Cindy";
return View();
}

public ActionResult Index1()
{
string MyName ;

MyName=this.TempData["MyName"].ToString ();
return View();
}


    页面绑定:和ViewData绑定一样。
 
    注意:当我们第一次访问Index页面之后,再访问Index1的时候,在Index1页面就会绑定上值“Cindy”,但当我们刷新Index1页面的时候,值就消失了。
 

    四、使用Model进行传值

 
    通过model来进行传值是通过强类型来进行绑定的,我们只需在View中通过Model对应的属性就可以获得相应的指。
    首先,我们在Model文件夹下,建立一个model,类名为PersonModel.cs。
      public partial class PersonModel
{
public string name { get; set; }
public int age { get; set; }
public string sex { get; set; }
}
    紧接着,在controller中写入:
    public ActionResult Index()
{
PersonModel person = new PersonModel();
person.age = 23;
person.name = "cindy";
person.sex = "gril";

return View(person);
}
   然后,在页面中通过Model的属性来获取属性的值。
    <h3>Name:@Model.name</h3>   
   最后,看效果图:
   



    五、AJAX代码进行Controller和View之间的传值

     
    Controller代码:
 
public JsonResult JsonDemo()
{
var person = new
{
Name = "Cindy",
Age = 23,
Sex = "Girl"
};
return Json(person);
}


     JS代码:
 

$(function () {
$.ajax({
type: "POST",
dataType: "json",
url: "/GetDataFrom/JsonDemo",
success: function (data) {
if (data.length < 1) {
alert("没有查到数据");
}
contentName = data.Name;
document.getElementById("name").innerHTML = "您的姓名为:"+contentName;

contentSex = data.Sex;
document.getElementById("sex").innerHTML = "您的性别为:"+contentSex;

contentAge = data.Age;
document.getElementById("age").innerHTML = "您的年龄为:" + contentAge;

},
});
});


    以上代码中的URL指的是controller中要传值的方法。data,为返回的数据。后面我们将返回的数据绑定到View中相应的id上即可显示数据。如果要将传值类型改为GET的话,我们就要在Controller返回值类型的参数中加入,
JsonRequestBehavior.AllowGe,要不然页面是无法获取值的。

      

总结:

    自己动手敲例子,虽然花了不少时间,但是也值了,相比第一次写来说,速度快了很多,可以自己写代码了。当我没有弄出来的时候,觉得这个东西怎么这么难啊,可是当我做出来的时候,我就又会觉得这个东西其实挺简单的,我怎么用了这么长时间啊。还是写代码写的少,以后要看API文档,多动手自己写代码。

    总结一句话:多动手实践,成功就离我不远了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: