ViewData是否有更好的解决方案

时间:2013-10-14 16:09:37

标签: asp.net vb.net asp.net-mvc-3 model

我有一个在模型RETLogModel上运行的视图,它从一个完全不同的连接字符串中拉出一个名为RecordsTaskViews的表。我需要根据我的模型收集的信息从RecordsTaskViews传递信息。目前我的功能如下:

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,是否有更有效的方法将数据传递给视图?

1 个答案:

答案 0 :(得分:2)

  

是否有更有效的方法将数据传递给视图?

是的,使用包含所有相关(组合)字段的ViewModel并将其绑定到View。

使用自动对象映射器(例如AutoMapper)将业务模型中的数据复制到ViewModel。