简体   繁体   English

Asp.Net MVC设置ViewModel属性值

[英]Asp.Net MVC setting ViewModel property value

I have a situation where I have to update a view model property based on some cases. 我有一种情况,我必须根据某些情况更新视图模型属性。 viewmodel property to update is IsPrintable base on IsPrintableFlag() result. 基于IsPrintableFlag()结果,要更新的viewmodel属性是IsPrintable。 Currently I am updating the property from controller like model.IsPrintable = model.IsPrintableFlag(items, pages); 目前,我正在从像model.IsPrintable = model.IsPrintableFlag(items,pages);这样的控制器中更新属性。

My question is Instead of updating Viewmodel property from Controller, do we have option to set value of property in viewmodel? 我的问题是,不是要从Controller更新Viewmodel属性,我们是否可以选择在viewmodel中设置属性的值?

ViewModel Code: ViewModel代码:

public bool IsPrintable { get; set; }

public bool IsPrintableFlag(IList<Items> items,IList<Pages> pages )
        {
            switch (id)
            {
                case 1:
                case 2:
                    if (!pages.Any())
                    {
                        return pages.Any();
                    }
                    break;

                case 3:
                    return false;
                default:
                    return false;
            }

            return false;
        }

Controller code: 控制器代码:

model.IsPrintable = model.IsPrintableFlag(items, pages);

A view model shouldn't really contain logic, it should only contain data required by the view. 视图模型不应真正包含逻辑,而应仅包含视图所需的数据。 Populating the view model in the controller is fine. 在控制器中填充视图模型很好。

I would recommend you introduce a service layer which wraps up this type of logic eg 我建议您引入一个服务层,它包装了这种类型的逻辑,例如

public class MyDomainService
{
    public bool IsPrintable(IList<Items> items, IList<Pages> pages)
    {
        return ...;
    }
}
...
model.IsPrintable = domainSvc.IsPrintable(items, pages);

Alternatively you could extend this to return a fully populated view model 或者,您可以扩展它以返回完全填充的视图模型

public MyViewModel GetViewModel(...)
{
    var model = ...;
    model.IsPrintable = this.IsPrintable(items, pages);
    return model;
}

Got Answer to my question and want to share so that it might be useful for many people who might face similar issue. 得到了我的问题的答案,并希望与他人分享,以便它对可能面临类似问题的许多人有用。

You could actually even set the values within your IsPrintableFlag() method so that you wouldn't need to return a boolean value as well using : 实际上,您甚至可以在IsPrintableFlag()方法中设置值,这样就不需要使用以下方法返回布尔值:

// Define this method within your ViewModel class
public void SetPrintableFlag(IList<Items> items, IList<Pages> pages)
{
      // Assuming id is a property of your ViewModel class
      switch (id)
      {
                case 1:
                case 2:
                    // If there aren't any pages
                    if (!pages.Any())
                    {
                        // Then set your flag to the opposite (you could likely return true here instead)
                        IsPrintableFlag = pages.Any();
                    }
                    break;
                case 3:
                default:
                    IsPrintableFlag = false;
      }
}

Controller Code 控制器代码

model.SetPrintableFlag(items,pages);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM