C# 访问嵌套viewmodel中的变量
我有以下ViewModel,我找不到访问变量以执行选择操作的方法C# 访问嵌套viewmodel中的变量,c#,entity-framework,join,asp.net-mvc-5,viewmodel,C#,Entity Framework,Join,Asp.net Mvc 5,Viewmodel,我有以下ViewModel,我找不到访问变量以执行选择操作的方法 public class ViewOptionValues { public Option Option { get; set; } public IEnumerable<OptionValue_SetVal> Values { get; set; } } public class OptionValue_SetVal { public OptionValue OptionValue { g
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; }
}
编辑
根据这些评论,我删除了ViewModelÒoptionvalueÒSetValue并放置了一组Òoptionvalue
控制器
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);
}
看法
如果我正确理解了您的问题,您正在尝试将单个对象分配给不允许的集合
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.
viewModel.Values=new List{new OptionValue_SetVal{};
viewModel.Values.First().SetValues=新列表{setval};
//假设只有一个选项value_SetVal保存SetVal。否则,作为一个简单的步骤,您可以在重构之前使用foreach循环使事情正常工作。
注意:作为更好的方法,您可以将这些默认集合初始化逻辑移动到类中的构造函数或@Jason提到的单独服务中
希望这能帮助您……“以下视图模型”、“以下内容”:您忘记发布代码了吗?是的。。将其发布到minute@ViniVasundharan你能详细说明一下,你得到了什么构建错误吗?似乎在if(op!=null)行之后提供了不完整的代码“op.OptionValues=”{…另外,请用您在代码中发布的确切变量名来完善您的问题。我之所以没有写这篇文章,是因为甚至Intellisense都没有向我推荐代码中的内容。@SivaGopal:我已经用其他模型类编辑了代码,并为我的aim添加了更详细的解释。我不确定我是否理解正确。您正在尝试即时更新更新OptionValue\u SetValue列表和OptionValue\u SetValue中的SetValue列表。我用新控制器和下面的错误回答了这个问题。@ViniVasundharan根据您更新的代码和错误,我看到您试图将集合/列表分配给一个不允许的对象。如果您认为获取一组“OptionValue”数据,然后您还需要在viewmodel中保存一组数据,或者只过滤并分配从db获取的所需单个“OptionValue”对象数据。如果我有错误,请纠正我:在我的viewmodel
ViewOptionValues
中,我有一个Option
和一组OptionValue\u SetVal
w它又有一个SetValues
的集合。所以我真的应该使用第二个Viewmodel吗,因为我的OptionValue
类中已经有了SetVal
的集合。所以我想从ViewOptionValue视图模型的对象中得到的是:Option
,一个OptionValue
的集合还有一组SetValues
。但是我的OptionValues
中已经有了SetValues
的集合。我是否让它变得多余?
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.