简体   繁体   English

如何在C#中比较ViewModel的两个实例的值相等性

[英]How do I compare two instances of a ViewModel for value equality in C#

So I have a ViewModel for searching my database. 所以我有一个用于搜索我的数据库的ViewModel。 I intend to refactor away the result set collection. 我打算重构结果集集合。 I don't think it belongs in this ViewModel. 我不认为它属于这个ViewModel。 All I want to do is when the user submits the page is compare the ViewModel passed to my Controller Action with the ViewModel stored in TempData. 我想要做的就是当用户提交页面时,将传递给我的Controller Action的ViewModel与存储在TempData中的ViewModel进行比较。 So I can tell if it is the SameSearch. 所以我可以判断它是否是SameSearch。

So how could that be done? 那怎么可能呢? considering I have many many many properties in my viewModel I'd prefer not to compare one by one. 考虑到我的viewModel中有很多很多属性,我宁愿不逐一比较。 And using Json.Encode to serialize them and compare seems "hacky" to me. 并使用Json.Encode序列化它们并比较对我来说似乎是“hacky”。

Also, if I do have to compare one by one. 另外,如果我必须逐一比较。 Should that be done by overriding .Equals() 应该通过重写.Equals()来完成

controller action Search() 控制器动作Search()

public ActionResult Search(SearchViewModel model)
{
    SearchViewModel savedSearch = null;

    if (Request["submit"].Equals("reset",StringComparison.OrdinalIgnoreCase))
    {
        TempData.Remove("filter");
        model = new SearchViewModel();
    }
    else if (TempData.ContainsKey("filter"))
    {
        savedSearch = (SearchViewModel)Convert.ChangeType(TempData["filter"], typeof(SearchViewModel));
    }

    var currentmodel = System.Web.Helpers.Json.Encode(model);
    var savedmodel = System.Web.Helpers.Json.Encode(savedSearch);

    if (savedSearch == null)
    {
        model.NewSearch = true;
    }

    //The search hasn't changed.
    if(currentmodel == savedmodel) 
    {
        model.isSameSearch = true;
    }
    else
    {
        model.isSameSearch = false;
    }
    //do more stuff
    //return view
}

ViewModel SearchViewModel ViewModel SearchViewModel

public class SearchViewModel
{

    public SearchViewModel()
    {
        Page = 1;
        BuildingSearch = new SearchBuildingViewModel();
        ParcelSearch = new SearchParcelViewModel();
        SalesSearch = new SearchSalesViewModel();
        PropertyFeatureSearch = new SearchPropertyFeaturesViewModel();
        ValuesSearch = new SearchValuesViewModel();
    } 

    /// <summary>
    /// Gets or sets a value indicating whether use advanced search.
    /// </summary>
    public bool UseAdvancedSearch { get; set; }
    public bool NewSearch { get; set; }
    public bool isSameSearch { get; set; }
    /// <summary>
    /// Gets or sets the page.
    /// </summary>
    [HiddenInput]
    [ScaffoldColumn(false)]
    public int Page { get; set; }

    [HiddenInput]
    [ScaffoldColumn(false)]
    public string SortOption { get; set; }

    #region Search View Models

    /// <summary>
    /// Gets or sets the building search.
    /// </summary>
    public SearchBuildingViewModel BuildingSearch { get; set; }

    /// <summary>
    /// Gets or sets the parcel search.
    /// </summary>
    public SearchParcelViewModel ParcelSearch { get; set; }

    /// <summary>
    /// Gets or sets the property feature search.
    /// </summary>
    public SearchPropertyFeaturesViewModel PropertyFeatureSearch { get; set; }

    /// <summary>
    /// Gets or sets the sales search.
    /// </summary>
    public SearchSalesViewModel SalesSearch { get; set; }

    /// <summary>
    /// Gets or sets the values search.
    /// </summary>
    public SearchValuesViewModel ValuesSearch { get; set; }

    #endregion

    /// <summary>
    /// Gets or sets the search results.
    /// </summary>
    [ScaffoldColumn(false)]
    public IPagination<ParcelResultItemViewModel> SearchResults { get; set; }
}

Implement IEquatable<T> in your ViewModel where T is the ViewModel class name, and create your own Equals method logic: 在ViewModel中实现IEquatable<T> ,其中T是ViewModel类名,并创建自己的Equals方法逻辑:

public bool Equals(ViewModel other)
{
    //compare properties, etc here
}

And override GetHashCode(): 并覆盖GetHashCode():

public override int GetHashCode()
{
    //you custom hash code algorithm
}

Consider using http://comparenetobjects.codeplex.com/ which will do a deep compare of objects. 考虑使用http://comparenetobjects.codeplex.com/ ,它将对对象进行深入比较。 You can even use NuGet to get it: http://www.nuget.org/packages/CompareNETObjects 您甚至可以使用NuGet来获取它: http ://www.nuget.org/packages/CompareNETObjects

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

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