如何将值从多个列表传递给ViewModel

发布于 2025-02-12 23:36:28 字数 901 浏览 1 评论 0原文

我有两个模型列表和主模型。我需要访问和存储价值到模型属性。不确定如何使用ViewModel进行操作?以前,我正在使用ViewData列表,现在我要测试ViewModel。

<---Model--->
public class Master
 {
   public List<Table1> T1 { get; set; }
   public List<Table2> T2 { get; set; }
 }
 
public class Table1
{
  public string sample1 { get; set; }
  public string sample1 { get; set; }
}

public class Table2
{
  public string sample1 { get; set; }
  public string sample2 { get; set; }
}


<--Controller-->
Master master  = new Master();
var getSamples = _db.dbSamples.Where(y => y.sample == "xxxx");

foreach(var row in T1)  <<---- I need help correcting this part. I believe this is not right...
  {
    foreach (var item in getSamples)
      {
        row.sample1 = item.A;
        row.sample2 = item.B;
      }
   }
return View(master);

I have two model list and master model. I need to access and store value to the model properties. not sure how to do this using a viewModel? previously I was using viewData list and now I wan to test viewModel.

<---Model--->
public class Master
 {
   public List<Table1> T1 { get; set; }
   public List<Table2> T2 { get; set; }
 }
 
public class Table1
{
  public string sample1 { get; set; }
  public string sample1 { get; set; }
}

public class Table2
{
  public string sample1 { get; set; }
  public string sample2 { get; set; }
}


<--Controller-->
Master master  = new Master();
var getSamples = _db.dbSamples.Where(y => y.sample == "xxxx");

foreach(var row in T1)  <<---- I need help correcting this part. I believe this is not right...
  {
    foreach (var item in getSamples)
      {
        row.sample1 = item.A;
        row.sample2 = item.B;
      }
   }
return View(master);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2025-02-19 23:36:29
 public class Master
 {
   public List<Table1> T1 { get; set; }
   public List<Table2> T2 { get; set; }
 }
 
public class Table1
{
  public string sample1 { get; set; }
  public string sample2 { get; set; }
}

public class Table2
{
  public string sample1 { get; set; }
  public string sample2 { get; set; }
}

        <--Controller-->
        Master master  = new Master();
        var getSamples = _db.dbSamples.Where(y => y.sample == "sample1");
        var table1List = new List<Table1>();
        var table2List = new List<Table2>();
        var masterData= new Master();
            foreach (var item in getSamples)
              {
                var data = new Table1();
                data.A = item.A;
                data.B = item.B;
                table1List.add(data);
              }
        master.T1 = table1List;
        var getSamples1 = _db.dbSamples.Where(y => y.sample == "sample2");
           foreach (var item in getSamples1)
              {
                var data2 = new Table2();
                data2.A = item.A;
                data2.B = item.B;
                table2List.add(data2);
              }
        master.T2 = table2List;
        return View(master);

我认为您要做的就是这样。如果不是这样,请详细说明这个问题。

更新的代码。相应地更改变量名称。

 public class Master
 {
   public List<Table1> T1 { get; set; }
   public List<Table2> T2 { get; set; }
 }
 
public class Table1
{
  public string sample1 { get; set; }
  public string sample2 { get; set; }
}

public class Table2
{
  public string sample1 { get; set; }
  public string sample2 { get; set; }
}

        <--Controller-->
        Master master  = new Master();
        var getSamples = _db.dbSamples.Where(y => y.sample == "sample1");
        var table1List = new List<Table1>();
        var table2List = new List<Table2>();
        var masterData= new Master();
            foreach (var item in getSamples)
              {
                var data = new Table1();
                data.A = item.A;
                data.B = item.B;
                table1List.add(data);
              }
        master.T1 = table1List;
        var getSamples1 = _db.dbSamples.Where(y => y.sample == "sample2");
           foreach (var item in getSamples1)
              {
                var data2 = new Table2();
                data2.A = item.A;
                data2.B = item.B;
                table2List.add(data2);
              }
        master.T2 = table2List;
        return View(master);

I assume that what you are trying to do is this. If it is not, please elaborate on the question.

Updated code. Change variable names accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文