0

我使用网络网格来显示员工姓名,以及针对每个员工的下拉列表中的项目。对于项目下拉列,我必须将日期显示为从 HTML 帮助程序返回的列标题。即按照下面的代码而不是列标题“SelectedDate”,我必须显示由 Html 帮助器 (DateTime)@Html.GetNextDate((DateTime)item.SelectedDate, 0)) 返回的值。

下面是视图的快照

    @{
       var grid = new WebGrid(Model.employeeProjectsMapper);
     }

    @grid.GetHtml(
    columns: grid.Columns(
     grid.Column("EmployeeName"),
     grid.Column(
     header: "SelectedDate",
        format:
        @<span>
        @{ var index = Guid.NewGuid().ToString(); }
        @Html.Hidden("employeeProjectsMapper.Index", index)
        @Html.Hidden("employeeProjectsMapper[" + index + "].EmployeeID", (Int64)item.EmployeeID)
        @Html.Hidden("employeeProjectsMapper[" + index + "].SelectedDate",  
       (DateTime)@Html.GetNextDate((DateTime)item.SelectedDate, 0))
        @Html.DropDownList("employeeProjectsMapper[" + index + "].SelectedProject",    
       Model.ProjectList)
        </span>
     )
    )
  )

任何帮助深表感谢。谢谢苏玛

4

1 回答 1

0

您可以向视图模型添加一个属性,您的视图是强类型的,并且将包含例如第一条记录的选定日期(因为您在评论部分说所有员工都具有相同的 SelectedDate 值):

public class MyViewModel
{
    public IEnumerable<SelectListItem> ProjectList { get; set; }
    public IEnumerable<EmployeeViewModel> employeeProjectsMapper { get; set; }

    public DateTime SelectedDate { get; set; }
}

然后在您的控制器操作中简单地填充此属性的值:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.ProjectList = ...
    model.employeeProjectsMapper = ...

    // set the SelectedDate property from the first employee record
    // that will be used as header in the selected project column
    model.SelectedDate = model.employeeProjectsMapper.First().SelectedDate;

    return View(model);
}

最后在您的视图中,您可以使用此属性使用自定义 HTML 帮助程序设置标题文本:

@model MyViewModel

@{
    var grid = new WebGrid(Model.employeeProjectsMapper);
}

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column("EmployeeName"),
        grid.Column(
            header: ((DateTime)Html.GetNextDate(Model.SelectedDate, 0)).ToShortDateString(),
            format:
                @<span>
                    @{ var index = Guid.NewGuid().ToString(); }
                    @Html.Hidden("employeeProjectsMapper.Index", index)
                    @Html.Hidden("employeeProjectsMapper[" + index + "].EmployeeID", (Int64)item.EmployeeID)
                    @Html.Hidden("employeeProjectsMapper[" + index + "].SelectedDate", (DateTime)Html.GetNextDate((DateTime)item.SelectedDate, 0))
                    @Html.DropDownList("employeeProjectsMapper[" + index + "].SelectedProject", new SelectList(Model.ProjectList, "Value", "Text", item.SelectedProject))
                </span>
        )
    )
)
于 2013-01-11T12:43:35.170 回答