我有以下視圖模型,我無法找到一個方法來訪問的變量進行選擇操作訪問變量
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; }
}
我已經加入模型類的選項,以明確它有一個選項值集合,我的選項類有一個SetValues集合。
我有兩個問題:
- 我真的需要像
OptionValue_SetVal
一個ViewModel? - 如何從我的控制器設置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?
控制器
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();
「下面的視圖模型」,「以下」:你忘了張貼代碼? –
是的..在一分鐘內發佈它 – Vini
@ViniVasundharan你能詳細說明,你會得到什麼樣的構建錯誤?看起來你提供了不完整的代碼「op.OptionValues =」if(op!= null){...之後......也請用你在代碼中發佈的確切變量名稱來優化你的問題。 –