Razor webgrid获取所选行数据

Razor webgrid获取所选行数据,razor,asp.net-mvc-5,entity-framework-6,webgrid,Razor,Asp.net Mvc 5,Entity Framework 6,Webgrid,在我的Razor视图中,我有以下webgrid: @{ var grid = new WebGrid(Model, canPage: false, selectionFieldName: "VendorClassID", canSort:false); } @grid.GetHtml( headerStyle: "header", htmlAttributes: new{id = "tableGrid"}, tableStyle: "webgrid",

在我的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;       
    } 
@{
var grid=new WebGrid(模型,canPage:false,selectionFieldName:“VendorClassID”,canSort:false);
}
@grid.GetHtml(
headerStyle:“header”,
htmlAttributes:new{id=“tableGrid”},
表样式:“webgrid”,
selectedRowStyle:“webgrid选定行”,
列:网格。列(
grid.Column(标题:“选择”,格式:@@item.GetSelectLink(“选择”),
网格列(“ClassID”、“ID”),
grid.Column(“ClassNum”、“ClassNum”),
网格栏(“说明”)
)
) 
@if(网格选择)
{
var x=@grid.SelectedRow;
} 

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

我找到了解决方案,通过使用

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

   //logic
}
VendorClassID是我的VendorClass的一个属性。该页面有其型号的供应商类别列表

型号:

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; }
}
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; }
    }
控制器:

 [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();
 }
[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();
        }
[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>
}
@{
ViewBag.Title=“ValidationResults”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
@ActionLink(“Back”、“History”、“Home”、null、new{@class=“form control”})
验证结果:
1.成功消息
}
@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>
}