使用参数显示(重定向)到 WebGrid 视图到视图上的 DropDownList
Displaying(redirecting) to View from WebGrid with Parameter to DropDownList on View
如何将 ID 从我的 WebGrid 传递到我的 ActionResult:
这是我的 WebGrid 代码:
@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>
}
我的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();
}
然后我想使用此 ID select 我指向的 View
中 DropDownList 中的一个项目,我也不确定该怎么做。
但现在在 URL 中传递了 id - 我不想要这个。我想将参数传递给我的 ActionResult 并希望我的 URL 保留 "clean" - 我该怎么做?
附带说明一下,我正在寻找 css 可以很好地设计我的 WebGrid 样式(可能是圆角等)- 这可能吗?
谢谢!
几个小时后,我终于让它工作了:
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);
}
(ProjectID 参数来自我的登录页面 WebGrid 选择)
View:
@Html.DropDownListFor(m => m.SelectedProjectID, new SelectList(Model.ProjectList, "Value", "Text", Model.SelectedProjectID), new { @class = "form-control" })
我希望这对其他人也有帮助。
如何将 ID 从我的 WebGrid 传递到我的 ActionResult:
这是我的 WebGrid 代码:
@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>
}
我的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();
}
然后我想使用此 ID select 我指向的 View
中 DropDownList 中的一个项目,我也不确定该怎么做。
但现在在 URL 中传递了 id - 我不想要这个。我想将参数传递给我的 ActionResult 并希望我的 URL 保留 "clean" - 我该怎么做?
附带说明一下,我正在寻找 css 可以很好地设计我的 WebGrid 样式(可能是圆角等)- 这可能吗?
谢谢!
几个小时后,我终于让它工作了:
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);
}
(ProjectID 参数来自我的登录页面 WebGrid 选择)
View:
@Html.DropDownListFor(m => m.SelectedProjectID, new SelectList(Model.ProjectList, "Value", "Text", Model.SelectedProjectID), new { @class = "form-control" })
我希望这对其他人也有帮助。