Asp.net Mvc Codeplex Preview 5 第三篇 实现Action参数传递繁杂类型 【转】

Asp.net Mvc Codeplex Preview 5 第三篇 实现Action参数传递繁杂类型 【转】

本文的环境是Asp.net Mvc Codeplex Preview 5

 

前文提到我们可以使用 Controller中的UpdateModel来获取 繁杂类型

例如

 

1
UpdateModel(x, Request.Form.AllKeys);

但是这里有些问题,当我们使用Request.Form.AllKeys时,提交的数据中有非x属性时,就会发生错误:

The model of type ‘MyModel‘ does not have a property named ‘Name2‘.

但是使用

1 
UpdateModel(x, 
new
[] {

“
IDX
“
, 
“
Name
“
});

这种形式,我们又会觉得它太过麻烦。

 

其实Asp.net Mvc为我们提供了一种很简单的传递复杂数据的方式,它类似于Monorail中的DataBinder:

我们完全可以通过以下方式来传递数据。例如

 view:

1 
    
<%
using (Html.Form(
“
home
“
, 
“
about
“
, FormMethod.Post)) {

%>


2 
    
<%
=
Html.TextBox(
“
my.ID
“
)
%>


3 
    
<%
=
Html.TextBox(
“
my.Name
“
)
%>


4 
    
<%
=
Html.SubmitButton()
%>


5 
    
<%
} 
%>

 controller:

        [AcceptVerbs(
“
post
“
)]
        

public
 ActionResult About([ModelBinder(
typeof
(MyModelBinder))]MyModel my) {

            ViewData[

“
Title
“
] 
=
my.Name 
+
 my.ID;
            

return
 View();
        }

 这样我们就可以从my中获取到Post过来的值了,这里的关键在于[ModelBinder(typeof(MyModelBinder))]

 

而 MyModelBinder的实现方法如下

 

 1 
using
 System;

 2 
using
 System.Collections.Generic;

 3 
using
 System.ComponentModel;

 4 
using
 System.Globalization;

 5 
using
 System.Linq;

 6 
using
 System.Web.Mvc;

 7 


 8 
///
 
<summary>


 9 
///
 这个类是根据Controller.UpdateModel方法更改而成

10 
///
 
</summary>


11 
public
 
class
 MyModelBinder : IModelBinder{


12 
    
#region
 IModelBinder 成员


13 


14 
    
public
 
object
 GetValue(ControllerContext controllerContext, 
string
 modelName, Type modelType,

15 
                           ModelStateDictionary modelState){


16 
        
object
 model 
=
 Activator.CreateInstance(modelType); 
//
将做为参数的类实例化了


17 
        IEnumerable
<
string
>
 keys 
=
 modelType.GetProperties().Select(c 
=>
 c.Name); 
//
得到该对象的属性的名的字符串数组,这里的结果应该为[“ID”,”Name”]


18 
        
string
 objectPrefix 
=
 modelName; 
//
这个就是,我的对象名叫my则会检查  name=”my.ID” name=”my.Name”的表单字段


19 


20 
        PropertyDescriptorCollection properties 
=
 TypeDescriptor.GetProperties(model); 
//
对象的属性的集合


21 
        var dictionary 
=
 
new
 Dictionary
<
string
, PropertyDescriptor
>
();

22 
        
foreach
 (
string
 str 
in
 keys){


23 
//
遍历属性的字符串集合即[“ID”,”Name”]


24 
            
if
 (
!
string
.IsNullOrEmpty(str)){


25 
                PropertyDescriptor descriptor 
=
 properties.Find(str, 
true
);

26 
                
if
 (descriptor 
==
 
null
){


27 
                    
throw
 
new
 ArgumentException(

28 
                        
string
.Format(CultureInfo.CurrentUICulture, 
“
无此属性{0},{1}
“
, 
new
 
object
[]{model.GetType().FullName, str}),

29 
                        
“
modelName
“
);

30 
                }

31 
                
string
 str3 
=
 
string
.IsNullOrEmpty(objectPrefix) 
?
 str : (objectPrefix 
+
 
“
.
“
 
+
 str); 
//
将对象名与属性名拼接,如my.ID


32 
                dictionary[str3] 
=
 descriptor;

33 
            }

34 
        }

35 
        
foreach
 (var pair 
in
 dictionary){


36 
            
string
 key 
=
 pair.Key;

37 
            PropertyDescriptor descriptor2 
=
 pair.Value;

38 
            
object
 obj2 
=
 ModelBinders.GetBinder(descriptor2.PropertyType).GetValue(controllerContext, key,

39 
                                                                                    descriptor2.PropertyType, modelState);

40 
            
if
 (obj2 
!=
 
null
){


41 
                
try
{


42 
                    descriptor2.SetValue(model, obj2); 
//
设置属性的值


43 
                    
continue
;

44 
                }

45 
                
catch
{


46 
                    
//
如果有使用验证Helepr则会显示在Html.ValidationSummary中


47 
                    
string
 errorMessage 
=
 
string
.Format(CultureInfo.CurrentCulture, 
“
验证失败{0}:{1}
“
, 
new
[]{obj2, descriptor2.Name});

48 
                    
string
 attemptedValue 
=
 Convert.ToString(obj2, CultureInfo.CurrentCulture);

49 
                    modelState.AddModelError(key, attemptedValue, errorMessage);

50 
                    
continue
;

51 
                }

52 
            }

53 
        }

54 
        
return
 model; 
//
最后 返回这个我们设置完属性的对象


55 
    }

56 


57 
    
#endregion


58 
}

 

这样我们就实现了 用Action的参数传递复杂类型。

 

当然,如果你连[ModelBinder(typeof(MyModelBinder))]都不想写了,想直接来以下写法,

1 
        [AcceptVerbs(
“
post
“
)]

2 
        
public
 ActionResult About(MyModel my) {


3 
            ViewData[
“
Title
“
] 
=
my.Name 
+
 my.ID;

4 
            
return
 View();

5 
        }

 

这个也是可以的不过你要在Application_Start中添加

ModelBinders.Binders.Add(typeof (MyModel), new MyModelBinder());

来表示二者的绑定关系。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110860.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux系统CUDA10.2+CUDNN安装教程

    Linux系统CUDA10.2+CUDNN安装教程Ubuntu18.04下安装CUDA10.2及对应的CUDNN的教程。

    2022年6月12日
  • 深入浅出JMS(一)——JMS简单介绍

    深入浅出JMS(一)——JMS简单介绍

    2021年11月24日
  • 树莓派 gpio usb_树莓派gpio编程

    树莓派 gpio usb_树莓派gpio编程概览树莓派最令人兴奋的特点之一是它有一个GPIO连接器可以用来接其他的硬件设备。GPIO连接器实际上是由许多不同类型的接口组成的:真正的GPIO(GeneralPurposeInputOutput,通用输入/输出)针脚,你可以用来控制LED灯的开和关。I2C(Inter-IntegratedCircuit)接口针脚,使你能够仅使用2个控制针脚连接硬件模块。SPI(SerialPeriph…

    2022年10月14日
  • NULL与nullptr

    传统意义上来说,c++把NULL、0视为同一种东西,有些编译器将NULL定义为((void*)0),有些将其定义为0.c++不允许直接将void*隐式的转化为其他类型,但是如果NULL被定义为((void*)0),当编译char*p=NULL;NULL只好被定义为0。还有:void  func(int);void  func(char*);如果NULL被定义为0,func(NUL…

    2022年4月6日
  • ssh 报Connection closed by 127.0.0.1 port 的解决办法

    ssh 报Connection closed by 127.0.0.1 port 的解决办法起了一个虚拟机,想要通过ssh从host连接。起虚拟机时加上如下命令行:-netdevuser,id=net0,hostfwd=tcp::2222-:22\-devicevirtio-net-pci,netdev=net0\这样就将host的2222端口转发给虚拟机的22端口。sshroot@localhost-p2222得到Connection……

    2022年9月25日
  • PrintWriter and BufferedWriter区别和使用「建议收藏」

    PrintWriter and BufferedWriter区别和使用「建议收藏」区别:BufferedWriter:将文本写入字符输出流,缓冲各个字符从而提供单个字符,数组和字符串的高效写入。通过write()方法可以将获取到的字符输出,然后通过newLine()进行换行操作。并且BufferedWriter只能对字符流进行操作。PrintWriter:相对于BufferedWriter的好处在于,如果PrintWriter开启了自动刷新,那么当PrintWri…

    2022年6月5日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号