2

In my Razor view, I have this 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;       
    } 

It's my understanding that when I click the generated "select" link, the page posts back and the URL gets a parameter "VendorClassID = selectrowindex" added on. However, the value of the parameter seems to be the index of the selected row, which isn't particularly useful to me. Is there anyway to have the parameter value be set to a value from the selected row (ClassID, etc)? The @grid.SelectedRow doesn't seem to know anything about the row's data either, but I am new to MVC so perhaps there is a way to get the row data from there?

Snicklefritz
  • 355
  • 2
  • 6
  • 17

2 Answers2

1

I found the solution, I got the data I wanted (VendorClassID), by using

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

   //logic
}

VendorClassID is an attribute of my VendorClass. The page has a list of VendorClasses for its Model

Snicklefritz
  • 355
  • 2
  • 6
  • 17
0

Model:

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; }
}

Controller:

 [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();
 }

Repository:

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;
            }

View:

@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> 

Controller:

[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();
        }

Model:

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; }
    }

View:

@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>

Controller:

[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();
        }

partial 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>
}
Sender
  • 6,660
  • 12
  • 47
  • 66
murali
  • 11
  • 1