c# - 访问嵌套 View 模型中的变量

标签 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 { 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 Values 集合,而我的 Option 类有一个 SetValues 集合。

我有两个问题:

  1. 我真的需要像 OptionValue_SetVal 这样的 ViewModel 吗? ?
  2. 如何从我的 Controller 设置 SetVal 对象的值?

我正在努力实现的目标

选择我能够通过 var op 实现的父类 Option 的所有 OptionValues .现在通过var setVal我正在尝试填充 IEnumerable<SetVal> for each选项值`。

我失败的事情

  1. 访问 SetVal 的 OptionValue 的 ID需要填充。
  2. 填充 SetVal

这是我的 Controller (它在为 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_SetValue and placed a collection of ÒptionValues相反。

Controller

    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>
}

在 View 中,我正在执行 Controller 逻辑(这是不正确的),但无法为 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.

注意:作为更好的方法,您可以将这些默认集合初始化逻辑移动到类中的构造函数或@Jason 提到的单独服务。

希望这对你有帮助...

关于c# - 访问嵌套 View 模型中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991282/

相关文章:

c# - 在 .Net 4.7.2 中导入 AngleSharp 时绑定(bind)重定向以某种方式尝试将 NetStandard 2 作为 dll 引入

c# - .Net 5 Web API CORS LocalHost

c# - EntityFramework ComplexType 引用 EntityType

c# - FluentAssertions Should().BeEquivalentTo 不比较 EF 动态代理上的运行时派生类型

php - 内部加入 laravel 5.2

sql - 从两个连接表中选择不在第三个表中的行

c# - 从 JSON 文件中提取数据的 Null 引用

c# - 与 if 条件运算符相关的查询

sql - 如何优化这个 AspNetIdenty/Entity Framework 生成的查询?

algorithm - 加入非常大的列表