【发布时间】:2015-10-07 11:41:45
【问题描述】:
我有以下 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 添加了模型类,以明确它有一个选项值的集合,而我的选项类有一个 SetValues 的集合。
我有两个问题:
- 我真的需要像
OptionValue_SetVal这样的 ViewModel 吗? - 如何从我的控制器设置 SetVal 对象的值?
我正在努力实现的目标
选择我能够通过var op 实现的父类选项的所有选项值。现在通过var setVal,我正在尝试填充IEnumerable<SetVal> for eachOptionValue`。
我失败的事情
- 访问需要为其填充
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;
}
}
}
编辑
根据 cmets,我已经删除了 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();
【问题讨论】:
-
"the following viewmodel", "the following": 你忘记贴代码了吗?
-
是的.. 一分钟后发布
-
@ViniVasundharan 您能否详细说明一下,您遇到了哪些构建错误?看来您在 if(op!=null){ 行之后提供了不完整的代码“op.OptionValues=” ...另外,请使用您在代码中发布的确切变量名称来完善您的问题。
-
我没有写它,因为即使是 Intellisense 也没有建议我在我的代码中有什么。
-
@SivaGopal:我已经用其他模型类编辑了代码,并为我的目标添加了更详细的解释
标签: c# entity-framework join asp.net-mvc-5 viewmodel