在我的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;
}
我的理解是当我点击生成的&#34;选择&#34;链接,页面回发,URL获取参数&#34; VendorClassID = selectrowindex&#34;加上。但是,参数的值似乎是所选行的索引,这对我来说并不是特别有用。无论如何将参数值设置为所选行(ClassID等)中的值? @ grid.SelectedRow似乎对行的数据一无所知,但我是MVC的新手,所以也许有办法从那里获取行数据?
答案 0 :(得分:1)
我找到了解决方案,我使用
获得了我想要的数据(VendorClassID)@if (grid.HasSelection)
{
var x = grid.SelectedRow.Value.VendorClassID;
//logic
}
VendorClassID是我的VendorClass的一个属性。该页面包含其Model
的VendorClasses列表答案 1 :(得分:0)
型号:
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>
}