所以我有一个 ViewModel 来搜索我的数据库。我打算重构掉结果集集合。我不认为它属于这个 ViewModel。我要做的就是当用户提交页面时,将传递给我的控制器操作的 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
}
视图模型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; }
}