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

时间:2013-06-20 14:32:44

标签: c# asp.net-mvc

所以我有一个用于搜索我的数据库的ViewModel。我打算重构结果集集合。我不认为它属于这个ViewModel。我想要做的就是当用户提交页面时,将传递给我的Controller Action的ViewModel与存储在TempData中的ViewModel进行比较。所以我可以判断它是否是SameSearch。

那怎么可能呢?考虑到我的viewModel中有很多很多属性,我宁愿不逐一比较。并使用Json.Encode序列化它们并比较对我来说似乎是“hacky”。​​

另外,如果我必须逐一比较。是否应该通过覆盖.Equals()

来完成

控制器操作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

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; }
}

2 个答案:

答案 0 :(得分:3)

在ViewModel中实现IEquatable<T>,其中T是ViewModel类名,并创建自己的Equals方法逻辑:

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

并覆盖GetHashCode():

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

答案 1 :(得分:0)

考虑使用http://comparenetobjects.codeplex.com/来进行对象的深度比较。您甚至可以使用NuGet来获取它:http://www.nuget.org/packages/CompareNETObjects