【问题标题】:Razor webgrid get selected row dataRazor webgrid 获取选定的行数据
【发布时间】:2015-02-14 04:20:38
【问题描述】:

在我的 Razor 视图中,我有这个 webgrid:

@{
    var grid = new WebGrid(Model, canPage: false, selectionFieldName: "VendorClassID", canSort:false);
}

 @grid.GetHtml(   
    headerStyle: "header",
    htmlAttributes: new{id = "tableGrid"},
    tableStyle: "webgrid", 
    selectedRowStyle: "webgrid-selected-row",              
    columns: grid.Columns(   
    grid.Column(header: "Select", format: @<text>@item.GetSelectLink("Select")</text>),
        grid.Column("ClassID", "ID"),
        grid.Column("ClassNum", "Class Num"),
        grid.Column("Description")
        )
        ) 

@if (grid.HasSelection)
    {
        var x = @grid.SelectedRow;       
    } 

据我了解,当我单击生成的“选择”链接时,页面会返回,并且 URL 会添加一个参数“VendorClassID = selectrowindex”。但是,参数的值似乎是所选行的索引,这对我来说并不是特别有用。无论如何要将参数值设置为所选行(ClassID等)中的值? @grid.SelectedRow 似乎对行数据一无所知,但我是 MVC 新手,所以也许有办法从那里获取行数据?

【问题讨论】:

    标签: razor asp.net-mvc-5 entity-framework-6 webgrid


    【解决方案1】:

    我找到了解决方案,我得到了我想要的数据 (VendorClassID),通过使用

    @if (grid.HasSelection)
    {
       var x = grid.SelectedRow.Value.VendorClassID; 
    
       //logic
    }
    

    VendorClassID 是我的 VendorClass 的一个属性。该页面有一个其模型的 VendorClasses 列表

    【讨论】:

      【解决方案2】:

      型号:

      public class SchemeDetailsModel
      {
          [Display(Name = "File Name")]
          public string FileName { get; set; }
          public Int64 FileId { get; set; }
          [DataType(DataType.Date)]       
          public DateTime Date { get; set; }
          [Display(Name = "Scheme Id")]
          public int SchemeId { get; set; }
          public string Status { get; set; }
          [Display(Name="Validation Error Report")]
          public string ValidationErrorReport { get; set; }
      }
      

      控制器:

       [HttpGet]
       public ActionResult History()
       {
           if (ModelState.IsValid)
           {
               List<SchemeDetailsModel> objSchemeDetails = new List<SchemeDetailsModel>();
               string employerId = Session["EmployerId"].ToString();
               objSchemeDetails = _repository.getSchemeDetails(employerId);
               return View(objSchemeDetails);
           }
               return View();
       }
      

      存储库:

      public List<SchemeDetailsModel> getSchemeDetails(string employerId)
              {
                  List<SchemeDetailsModel> objDetails = new List<SchemeDetailsModel>();
                  var query = (from efd in _context.EmployerFileDatas                        
                              //where efd.EmployerId == employerId 
                              orderby efd.FileName 
                              select new 
                              {
                                  efd.FileName,
                                  efd.Timestamp,
                                  //efhd.SchemeId,
                                  efd.ValidationStatus
                              }).ToList();
                  //join efhd in _dataContext.EmployerFileHeaderDetails on efd.EmployerId equals efhd.EmployerId
                  if(query!=null)
                  {
                      foreach (var item in query)
                      {
                          objDetails.Add(new SchemeDetailsModel { FileName = item.FileName, Date = item.Timestamp, Status = item.ValidationStatus, ValidationErrorReport = "View" });
                      }
                      return objDetails;
                  }
      

      查看:

      @model IEnumerable<EFITestHarness.Models.SchemeDetailsModel>
      @using System.Web.Helpers;
      @{
          ViewBag.Title = "SchemeDetails";
          Layout = "~/Views/Shared/_Layout.cshtml";
          var grid = new WebGrid(Model, canPage: true, rowsPerPage: 4, selectionFieldName:  "selectedRow");    grid.Pager(WebGridPagerModes.NextPrevious);
      }
      <table>
          <tr>
              <td>@Html.ActionLink("Back", "FileUpload", "Home", null, new { @class = "form-control" })
              </td>
          </tr>
      </table>
          <div id="gridContent" class="webGridWrapper">
          @grid.GetHtml(tableStyle: "webGrid",
                      footerStyle: "foot",
                      headerStyle: "webGridHeader",
                      alternatingRowStyle: "webGridAlt",
                      selectedRowStyle: "select",
                      columns: grid.Columns(
                      grid.Column("FileName"), //the model fields to display
                      grid.Column("Date"),
                      grid.Column("SchemeId"),
                      grid.Column("Status"),
                      grid.Column("ValidationErrorReport", format: (item => Html.ActionLink((string)(@item.ValidationErrorReport).ToString(), "ValidationResults", new { fileName = @item.FileName })))
               ))
      </div> 
      

      控制器:

      [HttpGet]
              public ActionResult ValidationResults(string fileName)
              {
                  Session["FileName"] = fileName;
                  if (ModelState.IsValid)
                  {
                     List<ValidationResultsModel> objValidationResults = new List<ValidationResultsModel>();
                     string employerId = Session["EmployerId"].ToString();
                     objValidationResults = _repository.getValidationResultsDetails(101);
                     if (objValidationResults.Count() > 0)
                     {
                         //var dataGrid = new GridView();
                         Session["results"] = objValidationResults;
                         //dataGrid.DataSource = objValidationResults;
                         //dataGrid.DataBind();
                        return View(objValidationResults);
                      }
                      else
                          return PartialView("~/Views/Results.cshtml");
                  }
                 return View();
              }
      

      型号:

      public class ValidationResultsModel
          {
              public string NINO { get; set; }
              public DateTime? DOB { get;set;}
              public string Transaction {  get;set; }
              public string Element {  get;set; }
              public string ErrorMessage { get; set; }
          }
      

      查看:

      @model IEnumerable<EFITestHarness.Models.ValidationResultsModel>
      @using System.Web.Helpers;
      @{
          ViewBag.Title = "ValidationResults";
          Layout = "~/Views/Shared/_Layout.cshtml";
          var grid = new WebGrid(Model, canPage: true, rowsPerPage:2, selectionFieldName:  "selectedRow");    grid.Pager(WebGridPagerModes.NextPrevious);
        }
      @using (Html.BeginForm("ValidationResults", "Home", new { enctype = "multipart/form-data" }))
      {
          @Html.ValidationSummary(true)    
              <table>
          <tr>
              <td>@Html.ActionLink("Back", "History", "Home", null, new { @class = "form-control" })
              </td>
          </tr>
      </table> 
         <div class="container-fluid" style="font-size:medium;color:blue;">
              Validation Results:   </div>
          <div style="font-size:medium;color:black;">1.Error Message<br />                              
          </div>
          <div id="gridContent" class="webGridWrapper">
              @grid.GetHtml(tableStyle: "webGrid",
                      footerStyle: "foot",
                      headerStyle: "webGridHeader",
                      alternatingRowStyle: "webGridAlt",
                      selectedRowStyle: "select",
                      columns: grid.Columns(
                  grid.Column("NINO"), //the model fields to display
                  grid.Column("DOB"),
                  grid.Column("Transaction"),
                  grid.Column("Element"),
                  grid.Column("ErrorMessage", style: "description")
      
               ))
          </div>
      }           
      <div style="font-size:medium;color:black;">
          Select the Button below to Download the Validation error Report
      </div>
      <div>
          <input type="button" class="btn btn-primary btn-md" value="Download" onclick="@("window.location.href='" + @Url.Action("Download", "Home") + "'");" />
      
      </div>
      

      控制器:

      [HttpGet]
              public ActionResult Download()
              {
                  var dataGrid = new GridView();
                  dataGrid.DataSource = Session["results"];
                  dataGrid.DataBind();
                  /*Code to eport the detail in excel sheet format*/
                  Response.ClearContent();
                  Response.Buffer = true;
                  Response.AddHeader("content-disposition", "attachment; filename='" + Session["FileName"] + "'.xls");
                  Response.ContentType = "application/ms-excel";
                  Response.Charset = "";
                  StringWriter sw = new StringWriter();
                  HtmlTextWriter htw = new HtmlTextWriter(sw);
                  dataGrid.RenderControl(htw);
                  Response.Output.Write(sw.ToString());
                  Response.Flush();
                  Response.End();
                  return View();
              }
      

      局部视图:

      @{
          ViewBag.Title = "ValidationResults";
          Layout = "~/Views/Shared/_Layout.cshtml";
      }
      
      @using (Html.BeginForm())
      {
          @Html.ValidationSummary(true)
          <table>
              <tr>
                  <td>
                      @Html.ActionLink("Back", "History", "Home", null, new { @class = "form-control" })
              </td>
          </tr>
      </table>
          <div class="container-fluid" style="font-size:medium;color:blue;">
              Validation Results:
          </div>
          <div style="font-size:medium;color:black;">
              1.Successful Message<br />
          </div>
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-23
        • 2012-11-01
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-02
        • 2011-12-18
        相关资源
        最近更新 更多