【发布时间】:2015-07-08 07:21:17
【问题描述】:
我有以下代码:
TryUpdateModel(model);
if (ModelState.IsValid && model.FullNameIsChecked == true && model.LookingFor == null)
{
model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
model.ContactList.Sort((x, y) => string.Compare(x.FullName, y.FullName));
}
else if (ModelState.IsValid && model.FullNameIsChecked == false && model.LookingFor == null)
{
model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
model.ContactList.Sort((x, y) => string.Compare(x.Email, y.Email));
}
else if (ModelState.IsValid && model.LookingFor != null && model.FullNameIsChecked == true)
{
model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
model.ContactList = contactRepository.GetAll(filter: x => x.FullName.Contains(model.LookingFor));
model.ContactList.OrderBy(f => f.FullName);
}
else if (ModelState.IsValid && model.LookingFor != null && model.FullNameIsChecked == false)
{
model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
model.ContactList = contactRepository.GetAll(filter: x => x.Email.Contains(model.LookingFor));
model.ContactList.OrderBy(e => e.Email);
}
else
{
model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
}
我正在使用带有 Razor 引擎的 .NET MVC 5,并使用上面的代码进行排序操作。我可以优化我的 If else 部分代码吗?我唯一想到的是使用 switch - case operator 但它看起来几乎相似。 提前致谢
【问题讨论】:
标签: c# .net model-view-controller