MVC 5创建新的ViewModel不保留属性设置

时间:2016-07-19 20:02:15

标签: c# asp.net-mvc

我正在为网站重构一些代码,我正在尝试减少重复代码。

我设置了一个ViewModel然后制作并覆盖它将填充属性。当我调试时,我可以单步执行并看到在创建新对象时它会填充但是一旦它返回到控制器就没有设置属性。我想我可以在创建新对象时填充这样的属性。

namespace VTC.Models.ViewModels
{
    public class VtcCommentViewModel
    {
        DataAccess DAL = new DataAccess();

        public string EmployeeName { get; set; }
        public string EmployeeNumber { get; set; }
        public string ReaderName { get; set; }
        public string Comment { get; set; }
        public string CommentStatus { get; set; }    
        public List<PunchList> PunchList { get; set; }


        public VtcCommentViewModel()
        { }

        public VtcCommentViewModel(Auth_Admin auth)
        {
            ValidETMResult validEtm = DAL.ValidateETM(auth.CurrentUser, auth.CurrentIP);

            VtcCommentViewModel vm = new VtcCommentViewModel();
            vm.EmployeeName = validEtm.emp_name;
            vm.EmployeeNumber = auth.CurrentUser;
            vm.ReaderName = validEtm.rdr_name;
            vm.PunchList = DAL.GetPunchList(auth.CurrentUser);
        }

    }
}

这是我的控制器。当我通过创建新的ViewModel进行调试时,我可以看到它填充属性,但当我查看View(vm)时,vm为空。

public ActionResult Comments()
{
    VtcCommentViewModel vm = new VtcCommentViewModel(auth);
    SetPageTitle("Enter Exception Log Comments");

    return View(vm);
}

更新

不确定这是不是很好的设计,但我将属性的填充从ViewModel构造函数移动到控制器中的函数。

这就是我现在在控制器中的功能,它似乎有效。

public ActionResult Comments()
{
    VtcCommentViewModel vm = populateCommentVM(auth);
    SetPageTitle("Enter Exception Log Comments");

    return View(vm);
}

private VtcCommentViewModel populateCommentVM(Auth_Admin auth)
{
    ValidETMResult validEtm = DAL.ValidateETM(auth.CurrentUser, auth.CurrentIP);

    VtcCommentViewModel vm = new VtcCommentViewModel();
    vm.EmployeeName = validEtm.emp_name;
    vm.EmployeeNumber = auth.CurrentUser;
    vm.ReaderName = validEtm.rdr_name;
    vm.Comment = string.Empty;
    vm.PunchList = DAL.GetPunchList(auth.CurrentUser);

    return vm;
}

3 个答案:

答案 0 :(得分:2)

在构造函数中创建新对象VtcCommentViewModel,并将属性分配给该新对象

    public VtcCommentViewModel(Auth_Admin auth)
    {
        ValidETMResult validEtm = DAL.ValidateETM(auth.CurrentUser, auth.CurrentIP);

        VtcCommentViewModel vm = new VtcCommentViewModel();
// here you are creating a new object - WRONG!
        vm.EmployeeName = validEtm.emp_name;
        vm.EmployeeNumber = auth.CurrentUser;
        vm.ReaderName = validEtm.rdr_name;
        vm.PunchList = DAL.GetPunchList(auth.CurrentUser);
    }

应该是这个

    public VtcCommentViewModel(Auth_Admin auth)
    {
        ValidETMResult validEtm = DAL.ValidateETM(auth.CurrentUser, auth.CurrentIP);

        // assign values to property of the object you are creating - not a new object
        EmployeeName = validEtm.emp_name;
        EmployeeNumber = auth.CurrentUser;
        ReaderName = validEtm.rdr_name;
        PunchList = DAL.GetPunchList(auth.CurrentUser);
    }

注意:您的View模型不应该知道您的DAL - 看起来像是一些糟糕的架构

答案 1 :(得分:0)

除了在你的虚拟机中包含DAL访问外,你在构造函数上还有一些缺陷:

        ValidETMResult validEtm = DAL.ValidateETM(auth.CurrentUser, auth.CurrentIP);

        // Wrong! You are creating a new instance which dies when you left the constructor!
        VtcCommentViewModel vm = new VtcCommentViewModel();
        vm.EmployeeName = validEtm.emp_name;
        vm.EmployeeNumber = auth.CurrentUser;
        vm.ReaderName = validEtm.rdr_name;
        vm.PunchList = DAL.GetPunchList(auth.CurrentUser);

修正:

        ValidETMResult validEtm = DAL.ValidateETM(auth.CurrentUser, auth.CurrentIP);

        // Wrong! You are creating a new instance which dies before you exit the constructor!
        // Nop VtcCommentViewModel vm = new VtcCommentViewModel();
        this.EmployeeName = validEtm.emp_name;
        this.EmployeeNumber = auth.CurrentUser;
        this.ReaderName = validEtm.rdr_name;
        this.PunchList = DAL.GetPunchList(auth.CurrentUser);

尝试获取一个帮助类,将您的数据加载到viewmodel中。

答案 2 :(得分:0)

也许值得考虑的是使用Factory Method PatternBuilder Pattern。另外,其他人已经告诉VM不应该知道DAL Separation of concern