码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC3中Controller与View之间的数据传递总结

时间:2015-01-01 23:54:51      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:asp.net   mvc   javascript   jquery   

</pre>在ASP.NET MVC<span style="font-family:宋体">中,经常会在</span>Controller<span style="font-family:宋体">与</span>View<span style="font-family:宋体">之间传递数据,因此,熟练、灵活的掌握这两层之间的数据传递方法就非常重要。本文从两个方面进行探讨:</span><p></p><p style="margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; text-indent:-21pt; font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><span style="font-size:14pt; font-family:Wingdings">Ø </span><strong><span style="font-size:14pt">Controller</span><span style="font-size:14pt; font-family:宋体">向</span><span style="font-size:14pt">View</span><span style="font-size:14pt; font-family:宋体">传递数据</span></strong></p><p style="margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; text-indent:-21pt; font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><span style="font-size:14pt; font-family:Wingdings">Ø </span><strong><span style="font-size:14pt">View</span><span style="font-size:14pt; font-family:宋体">向</span><span style="font-size:14pt">Controller</span><span style="font-size:14pt; font-family:宋体">传递数据</span></strong></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; line-height:25.2000007629395px"> </p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong><span style="font-size:24pt; font-family:宋体">一、</span><span style="font-size:24pt">Controller</span><span style="font-size:24pt; font-family:宋体">向</span><span style="font-size:24pt">View</span><span style="font-size:24pt; font-family:宋体">传递数据</span></strong></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px"></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; text-indent:-21pt"><strong><span style="font-size:14pt">1.<span style="font-family:'Times New Roman'; font-weight:normal; font-size:7pt; line-height:normal">       </span></span><span style="font-size:14pt; font-family:宋体">使用</span><span style="font-size:14pt">ViewData</span><span style="font-size:14pt; font-family:宋体">传递数据</span></strong></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px"><span style="font-family:宋体">我们在</span>Controller<span style="font-family:宋体">中定义如下:</span></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px">ViewData[“<span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>Message_ViewData</strong></span>”] = “ <span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>Hello </strong></span>ViewData!</strong></span>”;</p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px"><span style="font-family:宋体">然后在</span>View<span style="font-family:宋体">中读取</span>Controller<span style="font-family:宋体">中定义的</span>ViewData<span style="font-family:宋体">数据,代码如下:</span></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px">@Html.Encode(ViewData["Message_ViewData"])</p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px">js中读取<span style="font-family:Verdana; font-size:19px; line-height:25.2000007629395px; text-indent:-28px"><strong><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>ViewData中数据如下:</strong></span></strong></span></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px"><span style="font-family:Verdana; font-size:19px; line-height:25.2000007629395px; text-indent:-28px"><strong><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong><script type="text/javascript">    var viewData = '@ViewData["Message_ViewData"]';</script></strong></span></strong></span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px"></p><div style="text-indent:-28px"><span style="font-family:Verdana"><span style="font-size:14px; line-height:25.2000007629395px"><strong></strong></span></span></div><p></p><p style="margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; text-indent:-21pt"><span style="font-size:14pt">2.<span style="font-family:'Times New Roman'; font-weight:normal; font-size:7pt; line-height:normal">       </span></span><span style="font-size:14pt; font-family:宋体">使用</span><span style="font-size:14pt">ViewBag</span><span style="font-size:14pt; font-family:宋体">传递数据</span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px"><span style="font-family:宋体">我们在</span>Controller<span style="font-family:宋体">中定义如下:</span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px">ViewBag.Message_ViewBag =  “ <span style="line-height:25.2000007629395px"><span style="line-height:25.2000007629395px">Hello </span><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>ViewBag </strong></span>!</span>”;</p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px"><span style="font-family:宋体">然后在</span>View<span style="font-family:宋体">中读取</span>Controller<span style="font-family:宋体">中定义的</span><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>ViewBag</strong></span><span style="font-family:宋体">数据,代码如下:</span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px">@Html.Encode(<span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>ViewBag.</strong></span><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>Message_ViewBag</strong></span>)</p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px">js中读取<span style="font-size:19px; line-height:25.2000007629395px; text-indent:-28px"><span style="font-size:14px; line-height:25.2000007629395px"><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>ViewBag</strong></span>中数据如下:</span></span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px"><span style="font-size:19px; line-height:25.2000007629395px; text-indent:-28px"><span style="font-size:14px; line-height:25.2000007629395px"><script type="text/javascript">    var viewBag= '@<span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>ViewBag.Message_ViewBag</strong></span>';</script></span></span></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; text-indent:-21pt"><strong><span style="font-size:14pt">3.<span style="font-family:'Times New Roman'; font-weight:normal; font-size:7pt; line-height:normal">       </span></span><span style="font-size:14pt; font-family:宋体">使用</span><span style="font-size:14pt">TempData</span><span style="font-size:14pt; font-family:宋体">传递数据</span></strong></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px"><span style="font-family:宋体">我们在</span>Controller<span style="font-family:宋体">中定义如下:</span></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px">TempData[“Message”] = “Hello word!”;</p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px"><span style="font-family:宋体">然后在</span>View<span style="font-family:宋体">中读取</span>Controller<span style="font-family:宋体">中定义的</span>TempData<span style="font-family:宋体">数据,代码如下:</span></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px">@Html.Encode(TempData["Message_TempData"])</p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto; padding-top:0px; padding-bottom:0px"></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px">js中读取<span style="font-size:19px; line-height:25.2000007629395px"><span style="font-size:14px; line-height:25.2000007629395px"><span style="line-height:25.2000007629395px"><span style="font-family:Verdana; font-size:14px; line-height:25.2000007629395px"><strong>TempData</strong></span></span>中数据如下:</span></span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px"><span style="font-size:19px; line-height:25.2000007629395px; text-indent:-28px"><span style="font-size:14px; line-height:25.2000007629395px"><script type="text/javascript">     var tempData = '@TempData["Message"]';</script></span></span></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; text-indent:-21pt"><strong><span style="font-size:14pt">4.<span style="font-family:'Times New Roman'; font-weight:normal; font-size:7pt; line-height:normal">       </span></span><span style="font-size:14pt; font-family:宋体">使用</span><span style="font-size:14pt">Model</span><span style="font-size:14pt; font-family:宋体">传递数据</span></strong></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; text-indent:-21pt"><strong><span style="font-size:14pt; font-family:宋体">首先要创建Model实体类:</span></strong></p><p style="font-family:Verdana; font-size:14px; font-weight:bold; line-height:25.2000007629395px; margin:10px auto 10px 21pt; padding-top:0px; padding-bottom:0px; text-indent:-21pt"><strong><span style="font-size:14pt; font-family:宋体"></span></strong></p><pre code_snippet_id="569619" snippet_file_name="blog_20150101_2_4968406" name="code" class="csharp"> public class HelloModel
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _text;

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
    }



使用Model传递数据的时候,通常在创建View的时候我们会选择创建强类型View如下图所示:

技术分享

模型类下拉列表框中选中刚才创建的HelloModel

创建强类型的View以后,View的第一行代码如下所示:

@model Test.Models.HelloModel

就代表了这个View使用的Model为“Test.Models.HelloModel

 

然后在View中读取Model中定义的数据,代码如下:

@Html.Encode(Model.Name)

js中读取Model中数据如下:

<script type="text/javascript">
     var modelName = ‘@Model.Name‘;
</script>

总结:

1.         ViewDataTempData方式是弱类型的方式传递数据,而使用Model传递数据是强类型的方式。

2.         ViewDataTempData是完全不同的数据类型,ViewData数据类型是ViewDataDictionary类的实例化对象,而TempData的数据类型是TempDataDictionary类的实例化对象。

3.         TempData实际上保存在Session中,控制器每次执行请求时都会从Session中获取TempData数据并删除该SessionTempData数据只能在控制器中传递一次,其中的每个元素也只能被访问一次,访问之后会被自动删除。

4.         ViewData只能在一个Action方法中进行设置,在相关的视图页面读取,只对当前视图有效。理论上,TempData应该可以在一个Action中设置,多个页面读取。但是,实际上TempData中的元素被访问一次以后就会被删除。

5.  在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData。MVC3中保留了ViewData的使用,有关他们之间的区别可以参考这个文章。

    MVC3中 ViewBag、ViewData和TempData的使用和区别

 

二、ViewController传递数据

ASP.NET MVC中,将View中的数据传递到控制器中,主要通过发送表单的方式来实现。具体的方式有:

1.         通过Request.Form读取表单数据

我们在View层做如下定义:

    
@using (Html.BeginForm("HelloModelTest", "Home", FormMethod.Post))
{
    @Html.TextBox("Name");
    @Html.TextBox("Text");
    <input type="submit" value="提交" />
}

然后在Controller层,通过Request.Form读取表单数据的代码如下所示:

 [HttpPost]
        public ActionResult HelloModelTest()
        {
            string name= Request.Form["Name"];
            string text= Request.Form["Text"];
            return View();
        }



2.       通过FormCollection读取表单数据

我们在View层做如下定义:

@using (Html.BeginForm("HelloModelTest", "Home", FormMethod.Post))
{
    @Html.TextBox("Name");
    @Html.TextBox("Text");
    <input type="submit" value="提交" />
}

然后在Controller层,通过FormCollection读取表单数据的代码如下所示:

<span style="font-family:Verdana;">[HttpPost]
        public ActionResult HelloModelTest(FormCollection fc)
        {
            string name= fc["Name"];
            string text  = fc[</span><span style="font-family: Verdana; line-height: 25.2000007629395px; text-indent: 20.25pt;">"Text"];</span><span style="font-family:Verdana;">
            return View();
        }</span>

 3.       模型绑定

我们在View层做如下定义:

@using (Html.BeginForm("HelloModelTest", "Home", FormMethod.Post))
{
    @Html.TextBox("Name");
    @Html.TextBox("Text");
    <input type="submit" value="提交" />
}

默认的模型绑定

 相较于从请求中获取表单值,下面的Edit Action则是简单地以一个模型对像为参数(Album):

[HttpPost]
public ActionResult HelloModelTest( HelloModel model)
{
    // ...
}

当你的Action方法是以一个模型对象充当参数时,MVC运行时将会用模型绑定来构建该参数。默认用于模型绑定的是 DefaultModelBinder,以上述的HelloModel为例,DefaultModelBinder将会检索出所有可用的HelloModel属性用于绑定模型。根据命名约定,DefaultModelBinder能够自动地在请求中获取相应的值来填充HelloModel对象(它还能创建一个对象的实例来填充)

换句话说,假设HelloModel有一个Name属性,那么模型绑定就会在请求中查找名为Name的参数。注意我说的是在“请求中”,而不是“表单集合”中。模型绑定会在请求中的各个方面进行值查找,这里面包括路由数据,查询字符串,表单集合。有必要的话你还可以添加自定义的值提供信息。

 模型绑定不并局限于Http Post及复杂参数(如HelloModel),你完全可以传入一个原始的简单参数:

 public ActionResult HelloModelTest( string name,string text)
{
    // ….
}

在该场景中,模型绑定将会在请求中查找名为name,text 的参数。

显式模型绑定

当Action有参数的时候,会隐式地执行模型绑定。你还可以在控制器里面使用UpdateModel和 TryUpdateModel来显式调用模型绑定。调用UpdateModel的时候,如果模型对象是无效的或者绑定期间发生错误则会抛出异常。TryUpdateModel则不会抛出异常,它返回一个布尔值:如果绑定成功并且模型验证通过则返回true,否则返回false。

[HttpPost]
public ActionResult HelloModelTest( )
{
    HelloModel model = new HelloModel();
            
    if (this.TryUpdateModel(model))
    {
        //绑定成功
    }
    else
    {
	//绑定成功
    }
}


模型状态是模型绑定产生的副产物。每次绑定器绑定值到模型时,都会在模型状态中进行记录。你可以在模型绑定之后查看模型状态来判断绑定是否成功:

[HttpPost]
public ActionResult HelloModelTest( )
{
    HelloModel model = new HelloModel();
<pre name="code" class="csharp" style="color: rgb(75, 75, 75); line-height: 19.2000007629395px;">    this.TryUpdateModel(model);
if (ModelState.IsValid) { //绑定成功
} else {//绑定成功 }}


如果在模型绑定过程中发生异常,模型状态里面就会包含导致异常的那个属性名,绑定值以及错误信息。


ASP.NET MVC3中Controller与View之间的数据传递总结

标签:asp.net   mvc   javascript   jquery   

原文地址:http://blog.csdn.net/sknice/article/details/42323791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!