如何验证分离的ViewModel模型

时间:2015-04-29 17:53:17

标签: c# asp.net-mvc-4 razor

我想在ViewModel中使用ModelState.IsValid,里面有多个模型。

$ ifconfig 
docker0   Link encap:Ethernet  HWaddr 56:84:7a:fe:97:99  
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth0      Link encap:Ethernet  HWaddr 28:80:23:0a:3b:f7  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:20 Memory:d0700000-d0720000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:121430 errors:0 dropped:0 overruns:0 frame:0
          TX packets:121430 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:71329608 (71.3 MB)  TX bytes:71329608 (71.3 MB)

vboxnet10 Link encap:Ethernet  HWaddr 0a:00:27:00:00:0a  
          inet addr:172.28.128.1  Bcast:172.28.128.255  Mask:255.255.255.0
          inet6 addr: fe80::800:27ff:fe00:a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:616 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:169030 (169.0 KB)

wlan0     Link encap:Ethernet  HWaddr e8:2a:ea:97:84:ed  
          inet addr:192.168.0.102  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::ea2a:eaff:fe97:84ed/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1429759 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1428388 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:507751022 (507.7 MB)  TX bytes:878353259 (878.3 MB)

然后在我的行动中:

public class TestViewModel
{
    public TestViewModel()
    {
        UserModel = new UserDTO();
        PaymentModel = new PaymentDTO();
        ResultModel = new ResultDTO();
        NewUserModel = new NewUserDTO();
    }

    public UserDTO UserModel  { get; set; }
    public NewUserDTO NewUserModel  { get; set; }
    public PaymentDTO PaymentModel { get; set; }
    public ResultDTO ResultModel { get; set; }
}

在我的public ActionResult TestAction(TestViewModel model) { ModelState.IsValid //Just for UserDTO in some condition or just the NewUserDTO in other condition } 视图中强烈输入Payment.cshtml,如果是新用户,我会显示TestViewModel部分视图,其中强烈输入_Register.cshtml模型,如果是用户已经注册,我使用NewUserDTO模型强烈输入_User.cshtml部分视图。因此,当表单与新用户一起提交时,我必须忽略UserDTO验证。

有可能吗?

0 个答案:

没有答案