Refer to an other columns value in a MVC3 Razor WebGrid

℡╲_俬逩灬. 提交于 2020-01-06 04:20:28

问题


I have a webgrid presenting data grouped by weeks. The first grid column contains the weeknumber as seen below in the code example. The last "Summary" column needs to show different content depending on if the rows weeknumber match the current weeknumber or not. Is it possible to use the Weeknumber column value from them Summary column in some way?

@grid.GetHtml(tableStyle: "grid", headerStyle: "head",columns: trainingGrid.Columns(
  grid.Column("WeekNumber", "Week"),
  .....
  grid.Column("Summary", "Sum", format: (item) =>
    { if (***above columns weeknumber == ViewBag.CurrentWeekNo)
      { return "Dolor"
      }
      else{
        return "Sit";
      }
    }
)

回答1:


In the Column format method's parameter you get the current item as a dynamic object so you can access WeekNumber with normal property syntax:

grid.Column("Summary", "Sum", format: (item) =>
{
    if (item.WeekNumber == ViewBag.CurrentWeekNo)
    {
        return "Dolor";
    }
    else
    {
        return "Sit";
    }
}


来源:https://stackoverflow.com/questions/8532212/refer-to-an-other-columns-value-in-a-mvc3-razor-webgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!