目前我正在尝试检查所选的项目ID,并在编辑页面的下拉列表中显示,该页面将显示上一个选定的项目。项目根据其活动状态显示。所有活动项目都显示在下拉列表中,如果用户的上一个选择选项当前处于非活动状态,则它仍应默认为下拉列表中的该项目,因为它是先前所做的选择。我的问题是我无法显示用户在编辑页面上选择的非活动项目。我将发布所有代码,这是在另一个链接中提出的问题的延续:passing null parameters
我在控制器中的代码:
private IEnumerable<SearchItems> GetItems(ItemDescriptionFormViewModel viewModel = null)
{
if(viewModel == null)
viewModel = new AppointmentViewModel();
IOrderedQueryable<ItemDescription> items= _itemDescriptionRepository.FindAll().OrderBy(
c => c.Sort == null).ThenBy(
c => c.Sort).ThenBy(c => c.Description);
if(items.Count()==0)
ModelState.AddModelError("", string.Format("No active {0} entered.", Kids.Resources.Entities.ItemDescription.EntityNamePlural));
return
_itemDescriptionRepository.FindAll().OrderBy(c => c.Description).Where(a=>a.IsActive == true || a.ItemDescriptionId == viewModel.ItemDescriptionId).Select(
c => new SearchItems {Text = c.Description, Value = c.ItemDescriptionId.ToString()});
}
我在CONTROLLER中的编辑方法:
[HttpGet]
[AppointmentAuthorization]
public ActionResult Edit(Guid appointmentId)
{
Appointment appointment = _appointmentService.Get(appointmentId);
if (appointment == null) return View("NotFound");
var viewModel = new AppointmentViewModel
{
AppointmentId = appointment.AppointmentId,
};
viewModel.Items= GetItems();
return this.RazorView("Edit", viewModel);
}
似乎缺少某些东西并将viewModel作为参数传递给viewModel.Items = GetItems()并没有做太多。我还有另一个用于Edit的Post方法和一个调用GetItems()方法的Create方法。任何帮助都会很棒。谢谢:D
这就是我在ViewModel类中的内容:
public IEnumberable<SearchItems> Items {get; set;}
public Guid ItemDescriptionid {get; set;}
创建方法:
[HttpGet]
[AppointmentAuthorization]
public ActionResult Create(Guid caseId)
{
var viewModel = new AppointmentViewModel
{
Items= GetItems()
};
return this.RazorView("Create", viewModel);
}
***** SOLUTION *****
private IEnumerable<SearchItems> GetItems(Appointment appointment)
{
IEnumerable<short?> itemDescriptionIds =
appointment.AppointmentItems.Where(c => c.ItemDescriptionId.HasValue).Select(
c => c.ItemDescriptionId).Distinct();
IOrderedQueryable<ItemDescription> itemDescription =
_itemDescriptionRepository.FindAll().Where(
a => a.IsActive == true || itemDescriptionIds.Contains(a.ItemDescriptionId)).OrderBy(
d => d.Description);
return itemDescription.Select(c=> new SearchItems{Text = c.Description, Value = c.ItemDescriptionId.ToString()});
}
答案 0 :(得分:0)
您的初始代码未查询之前选择的值的原因是您的查询a.ItemDescriptionId == viewModel.ItemDescriptionId
,比较项目描述ID,但在您的View模型中,您只设置了约会ID。如果您需要项目描述ID,请确保将其初始化。
var viewModel = new AppointmentViewModel
{
AppointmentId = appointment.AppointmentId,
ItemDescriptionId = appointment.ItemDescriptionId
};
建议的重构:
private IEnumerable<SearchItems> GetItems(Guid lastId = new Guid())
{
IOrderedQueryable<ItemDescription> items= _itemDescriptionRepository.FindAll();
if(!items.Any())
ModelState.AddModelError("", string.Format("No active {0} entered.", Kids.Resources.Entities.ItemDescription.EntityNamePlural));
return items
.OrderBy(c => c.Description).Where(a=>a.IsActive == true || a.ItemDescriptionId == lastId)
.Select(c => new SearchItems {Text = c.Description, Value = c.ItemDescriptionId.ToString()});
}
当你使用它时所有人都使用必要的Guid时,传递ViewModel是很愚蠢的。然后把它称为
viewModel.Items= GetItems(appointment.ItemDescriptionId);
如果你的查询仍然没有添加正确的项目,请尝试调试它。
var testExists = itemDescriptionRepository.FindAll().FirstOrDefault(a.ItemDescriptionId == lastId);
如果您没有找到它,并且您确定lastId设置正确,则可能是数据库问题。