2010-09-28 174 views
0

我是MVC的初學者,想知道如何通過綁定模型設置返回查看。這裏是例子。模型綁定在MVC中查看

public class DataTypes 
{ 
    public Guid ItemID { get; set; } 
    [Required()] 
    public string Name { get; set; } 
    [Required()] 
    public string Status { get; set; } 
    [Required()] 
    public DataModel DataModel { get; set; } // This is for Binding 
} 
public class DataModel 
{ 
    public string Activity { get; set; } 
    public DateTime ?DateTime { get; set; }   
} 

通過上述模型類,我成功地能夠從UI數據綁定到後端,但問題是,我怎麼能使用上述retrun相同的數據的用戶界面。我想下面的代碼,但是,當它周圍來設置vaules的綁定類(DataModel的)

 this.dataType.ItemID = // Guid from stored vaule in DataBase 
     this.dataType.Name = // Name from stored vaule in DataBase 
     this.dataType.Status = // Status from stored vaule in DataBase 

         // Set the activity to UI - ERROR.....!!!!!! 
         // Error was NullReferenceException unhandled 
     this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase 
     this.dataType.DataModel.DateTime = // DateTime from stored vaule in DataBase 

     return View(this.dataType); 

任何工作,爲上述問題?

先感謝, HV

回答

1

看來,你忘了實例this.dataType.Datamodel:

this.dataType.DataModel = new DataModel(); 
this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase 
this.dataType.DataModel.DateTime 
+0

你是對的。謝謝您的幫助 – ary 2010-09-28 00:40:05