How to I pass an ID from my WebGrid to my ActionResult:
Here is my WebGrid code :
@using (Html.BeginForm("LandingPage", "ProjectDetail"))
{
var grid = new WebGrid(Model.Projectmodel, canPage: true, rowsPerPage: 3, defaultSort: "ProjectName");
<div id="gridContent" style="font-family: Arial; padding: 20px;" class="col-md-12">
@grid.GetHtml(tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
columns: grid.Columns(
grid.Column("ProjectName", format: (item) => item.GetSelectLink(item.ProjectName.ToString())),
grid.Column(columnName: "DesignerName", header: "Designer Name"),
grid.Column(columnName: "ProjectType", header: "Project Type"),
grid.Column(columnName: "FluidType", header: "Fluid Type"),
grid.Column(columnName: "PipeMaterial", header: "Pipe Material"),
grid.Column(header: "", format: (item) =>
{
var link = Html.ActionLink("View/Edit", "LandingPage", new { pkiProjectID = item.pkiProjectID }, new { @class = "btn btn-default2" });
return Html.Raw(link);
})), mode: WebGridPagerModes.All)
<h2>Project Details: </h2>
@if (grid.HasSelection)
{
var emp = (AirFlo_Size_Programme.Models.ProjectDetail)grid.Rows[grid.SelectedIndex].Value;
<p><b>Project Name:</b> @emp.ProjectName</p>
<p><b>Designer Name:</b> @emp.DesignerName</p>
<p><b>Project Type:</b> @emp.ProjectType</p>
<p><b>Fluid Type:</b> @emp.FluidType</p>
<p><b>Pipe Material:</b> @emp.PipeMaterial</p>
}
</div>
}
My ActionResult
:
public ActionResult ProjectInformation(ProjectInformationController model, int id)
{
if(!ModelState.IsValid)
{
return View(model);
}
using (RexusTradingEntities RTE = new RexusTradingEntities())
{
var ProjectData = (from PI in RTE.ProjectInformations.ToArray()
orderby PI.Project_Name
select new SelectListItem
{
Text = PI.Project_Name,
Value = PI.pkiProjectID.ToString()
}).ToArray();
ViewBag.ProjectList = ProjectData;
}
return View();
}
I then want to use this ID to select an item in a DropDownList in the View
I am directing to, which is something im also not sure how to do.
But now the id is passed in the URL - I dont want this. I want to pass the Parameter to my ActionResult and want my URL to stay "clean" - How do I do this ?
On a side note, im looking for css that can style my WebGrid nicely(Possibly rounded corners etc.) - is this possible ?
Thanks!
After many hours, I finally got it to work :
Model:
public class ProjectInformationViewModel
{
public int SelectedProjectID { get; set; }
public SelectList ProjectList2 { get; set; }
//Other Properties required for View added below
}
Controller:
public ActionResult ProjectInformation(ProjectDetailsViewModels model, int ProjectID)
{
if(!ModelState.IsValid)
{
return View(model);
}
var PIVM = new ProjectInformationViewModel();
using (RexusTradingEntities RTE = new RexusTradingEntities())
{
var ProjectData = (from PI in RTE.ProjectInformations.ToArray()
orderby PI.Project_Name
select new SelectListItem
{
Text = PI.Project_Name.ToString(),
Value = PI.pkiProjectID.ToString(),
Selected = true
}).ToArray();
PIVM.SelectedProjectID = ProjectID;
PIVM.ProjectList = new SelectList(ProjectData, "Value", "Text", ProjectID);
}
return View(PIVM);
}
(The ProjectID parameter is coming from my Landing Page WebGrid selection)
View:
@Html.DropDownListFor(m => m.SelectedProjectID, new SelectList(Model.ProjectList, "Value", "Text", Model.SelectedProjectID), new { @class = "form-control" })
I hope this helps someone else as well.