我有以下ViewModel,我找不到访问变量来执行选择操作的方法
public class ViewOptionValues
{
public Option Option { get; set; }
public IEnumerable<OptionValue_SetVal> Values { get; set; }
}
public class OptionValue_SetVal
{
public OptionValue OptionValue { get; set; }
public IEnumerable<SetValue> SetValues { get; set; }
}
public class Option
{
public int OptionID { get; set;}
public string OptionName { get; set; }
public int LsystemID { get; set; }
public virtual ICollection<OptionValue> OptionValues { get; set; }
}
public class OptionValue
{
public int OptionValueID { get; set; }
public string OptionVal { get; set; }
public int OptionID { get; set; }
public virtual Option Option { get; set; }
public virtual ICollection< SetValue> SetValue { get; set; }
}
public class SetValue
{
public int SetValueID { get; set; }
public string Value { get; set; }
public bool Status { get; set; }
public int OptionValueID { get; set; }
public virtual OptionValue OptionValue { get; set; }
}
我添加了Option的模型类,以明确它有一个Option of Option Values,我的Option类有一个SetValues集合。
我有两个问题:
OptionValue_SetVal
?我想要实现的目标
选择我可以通过var op
实现的父类选项的所有OptionValues。现在通过var setVal
我试图填充IEnumerable<SetVal> for each
OptionValue`。
我失败的事情
SetVal
的OptionValue的ID。SetVal
这是我的控制器(它在为SetVal添加值的行中存在构建错误)
public ActionResult ViewOptionvalues(int id)
{
var viewModel = new ViewOptionValues();
if (id != 0)
{
var op = db.Option.Include(x => x.OptionValues).FirstOrDefault(x => x.OptionID == id);
if(op!=null)
{
op.OptionValues=
var setval = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == op.OptionID);
viewModel.Values = setval.SetVal;
viewModel.OptionValues = op.OptionValues;
}
}
}
修改
根据评论,我删除了ViewModelÒptionValue_SetValueand placed a collection of ÒptionValues
。
控制器
public ActionResult ViewOptionValues(int id)
{
var viewmodel = new Option_OptionValues();
var op = db.Option.Include(x => x.OptionValues).FirstOrDefault(x => x.OptionID == id);
var set = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == id); //set is being populated, but I am not sure what need to be placed instead of id in this line
if(op!=null)
{
viewmodel.OptionValues = op.OptionValues;
}
return View(viewmodel);
}
查看
@foreach (var item in Model.OptionValues)
{
<tr>
<td rowspan="@item.SetValue.Count">@item.OptionVal</td>
<td rowspan="@item.SetValue.Count">@item.OptionValueID</td>
@{var set = item.SetValue.FirstOrDefault(x => x.OptionValueID == item.OptionValueID);}
@if (set != null)
{
for (int i = 0; i < item.SetValue.Count; i++)
{
<tr>
<td>@set.TcSet.SetName</td>
<td> @set.Value</td>
<td>@set.Status</td>
</tr>
}
}
</tr>
}
在视图中我正在执行控制器逻辑(这是不正确的),但无法获得SetValue的不同值。计数是正确的,但我得到的值是相同的。
修改
添加了联接
var set = (from o in db.Option
join ov in db.OptionValue on o.OptionID equals ov.OptionID
join s in db.SetValue on ov.OptionValueID equals s.OptionValueID
where o.TechnicalCharacteristicID == s.TcSet.TechnicalCharacteristicID
select ov.SetValue).SingleOrDefault();
答案 0 :(得分:1)
如果我正确理解您的问题,您正在尝试将单个对象分配给不允许的集合。
var setval = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == op.OptionID); //Notice FirstOrDefault(...)
作为一种快速(当然是脏!)的解决方法,您可以尝试以下方法:
viewModel.Values = new List<OptionValue_SetVal> {new OptionValue_SetVal{} };
viewModel.Values.First().SetValues = new List<SetValue> { setval };
//Assuming you have only one OptionValue_SetVal that hold setVal. Otherwise as a simple step you can go with foreach loop to make things work before you refactor.
注意:作为一种更好的方法,您可以将这些默认集合初始化逻辑移动到类中的构造函数或@Jason所提及的单独服务中。
希望这能帮到你......