2012-04-24 52 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個自定義構造函數。

因此,繼續前進,並重新審視自己的視圖模型的設計。可以使用繼承,但不使用自定義構造函數。