2012-04-24 48 views
0

在我的MVC应用程序,我已经定义了一个视图模型,如:形式系列化问题

public class Test 
    { 
     public Test(Models.Test1 t1) 
     { 
//set the properties for t1 
} 

public Test(Models.Test1 t1, Models.Test1 t2) 
      :this(t1) 
{ 
//set properties for t2 
} 

} 
// properties for t1 and t2 
} 

TestModel在我看来是用来显示来自T1结合领域:如

public class TestModel : Test 
    {   
     public TestModel (Models.Test1 t1) 
      :base(t1) 
     { } 
     public TestModel (Models.Test1 t1, Models.Test1 t2) 
      :base(t1,t2) 
     { } 

类检验定义和t2。当我提出这样的形式:

$('form').submit(function (evt) {     
      Save($(this).serialize(), 
      function() { 
       $('.loading').show(); 
      }, 
      function() { 
       alert('success'); 
      }); 
     }); 
     $('a.save').click(function (evt) {     
      $(this).parents('form').submit(); 
     }); 



- the controller action below is never hit. 

    [HttpPost]   
      public JsonResult Save(TestModel camp) 
      {       
        Helper.Save(camp); 
        return Json(JsonEnvelope.Success());   
      } 

我认为序列化是行不通的,因为从TestModel派生测试。任何关于如何使这项工作的建议?

回答

1

我认为序列化不起作用,因为TestModel从Test中派生 。任何关于如何使这项工作的建议?

序列化不工作,因为你的TestModel没有参数的构造函数。默认的model binder不知道如何实例化这个类。只有参数构造函数的类应作为视图模型。否则你就必须编写自定义的模型绑定到表示要使用的2个自定义构造函数。

因此,继续前进,并重新审视自己的视图模型的设计。可以使用继承,但不使用自定义构造函数。