0

首先,请注意我对此有点陌生。我想知道,如何使用来自两个模型(两个表)的列制作 Webgrid。

我有两个模型,像这样: Model1:

public int Id { get; set; }
public string Name { get; set; }

型号2:

public int Id { get; set; }
public int Model1_Id { get; set; }
public string Level { get; set; }
public string AdditionalInfo { get; set; }
public string Note { get; set; }

在控制器中处理数据并发送到视图。

...
List<Model2> data = new List<Model2>();
...
return View(data)

现在开始问题。在视图中,我正在从模型创建 Webgrid。一旦出现问题,列的名称必须与属性名称相同,否则就会失败。

@model IEnumerable<Model2>

<div class="class-name">
    @grid.GetHtml(columns: new [] {
    grid.Column("Level"),
    grid.Column("AdditionalInfo", header: "Additional info"),
    grid.Column("Note")
    }, tableStyle: "some-name")
</div>

没关系,但我希望在该网格中也有来自 Model1 的名称,并且值将取决于 Model1_Id。

如果我尝试通过 grid.Column 添加新列,它会下降。

如果我添加第二个模型(在第一行之后),它会写入“文件中只允许一个'模型'语句”。

我尝试过的所有东西都掉了。

我尝试与谷歌合作,但几个小时后,我想问你们。谢谢

4

2 回答 2

1

您可以使用两个模型创建 ViewModel

public class MyViewModel
{
    public Model1 Model1Data { get; set; }
    public List<Model2> Model2List { get; set; }
}

In Controller

    MyViewModel model = new MyViewModel();
    model.Model2List = new List<Model2>()
    model.Model1Data = new Model1() 

在你看来,

@model MyViewModel 
于 2013-08-27T14:17:56.533 回答
0

尝试这个,

这只是一个例子:

模型

  public class EmployeeDetailsModel
    {
        public string EnteredValue { get; set; }
        public int EmployeeId { get; set; }
    }
  public class SampleModel
    {
        public int inx { get; set; }
        public bool studentclass { get; set; }
        public string SampleDescription { get; set; }
        public string SampleCode { get; set; }
        public string SampleItems { get; set; }
        private EmployeeDetailsModel _employee = new EmployeeDetailsModel();
        public EmployeeDetailsModel employee { get { return _employee; } set { _employee = value; } }
    }

控制器

 public ActionResult IndexJquery()
        {
            ViewBag.RegisterItems = GetSamples();
            return View();
        }
  public IEnumerable<SampleModel> GetSamples()
        {
            List<SampleModel> sampleAdd = new List<SampleModel>();
            SampleModel s12 = new SampleModel();
            s12.inx = 1;
            s12.SampleCode = "123se";
            s12.SampleDescription = "GOOD";
            s12.SampleItems = "newone";
            s12.employee.EmployeeId = 1;
            s12.employee.EnteredValue = "jaimin";


            SampleModel s2 = new SampleModel();
            s12.inx = 2;
            s2.SampleCode = "234se";
            s2.SampleDescription = "Average";
            s2.SampleItems = "oldone";
            s2.employee.EmployeeId = 2;
            s2.employee.EnteredValue = "jaimin2";


            sampleAdd.Add(s12);
            sampleAdd.Add(s2);

            return sampleAdd;
        }

看法

 <div>
        @{
            var grid = new WebGrid(ViewBag.RegisterItems);
            @grid.GetHtml(
                 alternatingRowStyle: "alt",
                  columns: grid.Columns(
                    grid.Column("SampleCode"),
                    grid.Column("SampleDescription"),
                    grid.Column("SampleItems"),
                    grid.Column("employee.EmployeeId", header: "EmployeeId"),
                    grid.Column("employee.EnteredValue", header: "EnteredValue")
                 ));
        }
    </div>
于 2013-08-27T12:56:18.250 回答