两个模型中的列在一个webgrid中

时间:2013-08-27 11:26:04

标签: asp.net-mvc razor

首先,请注意我有点新鲜。我想知道,如何使用两个模型(两个表)中的列来创建Webgrid。

我有两个模特,比如: 型号1:

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添加新列。

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

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

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

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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>