访问嵌套视图模型中的变量
Accessing variables in a nested viewmodel
我有以下 ViewModel,但我找不到访问变量以执行 select 操作的方法
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 添加了模型 class 以明确它有一个 Option Values 集合,而我的 Option class 有一个 SetValues 集合。
我有两个问题:
- 我真的需要
OptionValue_SetVal
这样的 ViewModel 吗?
- 如何从我的控制器设置 SetVal 对象的值?
我想要达到的目标
Select 我可以通过 var op
实现的父 class 选项的所有选项值。现在通过 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();
如果我对您的问题的理解正确,您是在尝试将单个对象分配给不允许的集合。
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.
注意: 作为一种更好的方法,您可以将这些默认集合初始化逻辑移动到 class 中的构造函数或@Jason 提到的单独服务。
希望对您有所帮助...
我有以下 ViewModel,但我找不到访问变量以执行 select 操作的方法
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 添加了模型 class 以明确它有一个 Option Values 集合,而我的 Option class 有一个 SetValues 集合。
我有两个问题:
- 我真的需要
OptionValue_SetVal
这样的 ViewModel 吗? - 如何从我的控制器设置 SetVal 对象的值?
我想要达到的目标
Select 我可以通过 var op
实现的父 class 选项的所有选项值。现在通过 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();
如果我对您的问题的理解正确,您是在尝试将单个对象分配给不允许的集合。
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.
注意: 作为一种更好的方法,您可以将这些默认集合初始化逻辑移动到 class 中的构造函数或@Jason 提到的单独服务。
希望对您有所帮助...