【问题标题】:Displaying(redirecting) to View from WebGrid with Parameter to DropDownList on View从 WebGrid 显示(重定向)到视图,参数为视图上的 DropDownList
【发布时间】:2016-09-14 16:37:15
【问题描述】:

如何将一个 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 在我指向的 View 中的 DropDownList 中选择一个项目,这也是我不知道该怎么做的事情。

但现在 id 在 URL 中传递 - 我不想要这个。我想将参数传递给我的 ActionResult 并希望我的 URL 保持“干净” - 我该怎么做?

顺便说一句,我正在寻找可以很好地设置我的 WebGrid 样式的 css(可能是圆角等) - 这可能吗?

谢谢!

【问题讨论】:

    标签: asp.net-mvc-5 webgrid


    【解决方案1】:

    经过几个小时,我终于开始工作了:

    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" })
    

    我希望这对其他人也有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2016-08-22
      • 1970-01-01
      相关资源
      最近更新 更多