2013-10-14 80 views
0

我有一個模型RETLogModel上運行的視圖,它從一個完全不同的連接字符串,然後是一個名爲RecordsTaskViews表。我需要根據我的模型收集的信息從RecordsTaskViews傳遞信息。目前我的函數如下:有沒有更好的解決方案,然後ViewData

Function RETLog(model As RETModels.RETLogModel) As ActionResult 
    Dim id As Integer = model.ActivityIDReturn 
    Dim id2 As Integer 
    Dim PS As RecordsTaskView = GlobalVar.db.PS.RecordsTaskViews.Find(id) 
    If model.ActivityIDReturn > 0 Then 
     id2 = model.ActivityIDReturn 
    Else : id2 = PS.RefActionID 
    End If 
    If model.ActivityIDError > 0 Then 
     ViewData("ActivityIDErrorValue") = model.ActivityIDError 
    Else : ViewData("ActivityIDErrorValue") = PS.RefActionID 
    End If 
    Dim PS2 As RecordsTaskView = GlobalVar.db.PS.RecordsTaskViews.Find(id2) 
    If model.ActivityIDReturn > 0 Then 
     If PS IsNot Nothing Then 
      ViewData("QRefActionID") = PS.RefActionID 
      ViewData("QQutDesc") = PS.QutDesc 
      ViewData("QTaskDesc") = PS.TaskDesc 
      ViewData("QClientCode") = PS.CltCode 
      ViewData("QClientName") = PS.CltDesc 
      ViewData("QMemberID") = PS.BenIDin 
      ViewData("QMemberName") = PS.BenNameLast & ", " & PS BenNameFirst 
      ViewData("QDateStart") = CDate(PS.DateStart).ToString("MM/dd/yyyy") 
      ViewData("QDateRes") = CDate(PS.DateResolution).ToString("MM/dd/yyyy") 
      ViewData("QCreateUser") = PS.UserIDCreation 
      ViewData("QLastChangeUser") = PS.UserIDLastChanged 
      ViewData("QDateReport") = CDate(PS.DateReport).ToString("MM/dd/yyyy") 
     Else 
      ViewData("QQutDesc") = "" 
      ViewData("QTaskDesc") = "" 
      ViewData("QClientCode") = "" 
      ViewData("QClientName") = "" 
      ViewData("QMemberID") = "" 
      ViewData("QMemberName") = "" 
      ViewData("QDateStart") = "" 
      ViewData("QDateRes") = "" 
      ViewData("QCreateUser") = "" 
      ViewData("QLastChangeUser") = "" 
      ViewData("QDateReport") = "" 
     End If 
     If PS2 IsNot Nothing Then 
      ViewData("ORefActionID") = PS2.RefActionID 
      ViewData("OQutDesc") = PS2.QutDesc 
      ViewData("OTaskDesc") = PS2.TaskDesc 
      ViewData("OClientCode") = PS2.CltCode 
      ViewData("OClientName") = PS2.CltDesc 
      ViewData("OMemberID") = PS2.BenIDin 
      ViewData("OMemberName") = PS2.BenNameLast & ", " & PS.BenNameFirst 
      ViewData("ODateStart") = CDate(PS2.DateStart).ToString("MM/dd/yyyy") 
      ViewData("ODateRes") = CDate(PS2.DateResolution).ToString("MM/dd/yyyy") 
      ViewData("OCreateUser") = PS2.UserIDCreation 
      ViewData("OLastChangeUser") = PS2.UserIDLastChanged 
      ViewData("ODateReport") = CDate(PS2.DateReport).ToString("MM/dd/yyyy") 
     Else 
      ViewData("ORefActionID") = "" 
      ViewData("OQutDesc") = "" 
      ViewData("OTaskDesc") = "" 
      ViewData("OClientCode") = "" 
      ViewData("OClientName") = "" 
      ViewData("OMemberID") = "" 
      ViewData("OMemberName") = "" 
      ViewData("ODateStart") = "" 
      ViewData("ODateRes") = "" 
      ViewData("OCreateUser") = "" 
      ViewData("OLastChangeUser") = "" 
      ViewData("ODateReport") = "" 
     End If 
    End If 
    If ModelState.IsValid Then 
    End If 
    Return View(model) 
End Function 

我的問題是,這是明顯ViewData元素的船負荷。正如我已經有在視圖中指定的@ModelType RETModels.RETLogModel本身,是否有更高效的方式將數據傳遞給視圖?

回答

2

是否有更高效的方式將數據傳遞給視圖?

是的,使用包含所有相關(組合)字段並將其綁定到視圖的ViewModel。

然後使用自動對象映射器(如AutoMapper)將數據從業務模型複製到ViewModel。

相關問題