MVC 4 DropdownList设置选定值并通过ActionLink将值传递给控制器

时间:2014-01-06 19:04:08

标签: asp.net-mvc-4 parameters controller actionlink

我有以下观点。

    @model Scheduling.ViewModel.ReportsViewModel
    @Styles.Render("~/Content/themes/base/minified/jquery-ui.min.css")
    @Scripts.Render("~/Scripts/Reports.js")
    @{
        ViewBag.Title = "Index";
    }

    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />

   @using (Html.BeginForm("ViewReport","Report", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Reports</legend>
        <div class="editor-field">
          @Html.DropDownListFor(model => model.SelectedReport, Model.ReportList, new { id = "ui_ddlReportsList", style = "width: 25%;" })
        </div>
        <div id ="ui_div_BroadcastReportFilters">
           @Html.Label("Start Date:")
            @Html.EditorFor(model => model.StartDate, new{id = "ui_txt_StartDate" })


        </div>
        <p>
            <input id="ui_btn_RunReport" type="submit" value="Run Report" />  

        </p>

    </fieldset>
}

在我的控制器中我有

命名空间Scheduling.Controllers {     public class ReportController:Controller     {

    private SchedulingEntities db = new SchedulingEntities();
    //
    // GET: /Report/

     public ActionResult Index()
{
    var reportViewModel = new ReportsViewModel();


    return View("Index", reportViewModel);
}    

    /// <summary>
    /// Start method
    /// </summary>
    /// <returns></returns>
    public ActionResult Start()
    {
        return Index();
    }

  public ActionResult ViewReport(ReportsViewModel reportsViewModel)
{

    //ViewData["ArReport"] =
    Reports.ReportList report = 0;
    //if (Enum.IsDefined(typeof(Reports.ReportList), reportsViewModel.SelectedReport.pkReportID))
    //{
    //    report = ((Reports.ReportList)reportsViewModel.ReportID);

    //}



    var arReport = GenerateReport.RunReport(report);
    ViewBag.ArReport = arReport;
    return View("ViewReport"); //, new ReportDescriptor { Id = id, Type = reporttype });
}

}

}

我的视图模型如下

public class ReportsViewModel
    {
        private SchedulingEntities db = new SchedulingEntities();

        public Reports SelectedReport { get; set; }

        public SelectList ReportList { get; set; }

        public string StartDate { get; set; }

        public string EndDate { get; set; }

        public ReportsViewModel()
        {
            PopulateReports();

        }

        public void PopulateReports()
        {
            var reports = db.Reports.Where(r => r.IsActive).OrderBy(r => r.ReportOrder).ToList();
            if (reports.Any())
            {
                SelectedReport = reports[0];
            }
            ReportList = new SelectList(reports,"pkReportID","ReportName", SelectedReport);// db.Reports.Where(r => r.IsActive).OrderBy(r => r.ReportOrder).ToList();



        }


    }

更新确定我现在正在使用视图模型。我可以填充所有的editFor Fields,但是当它到达控制器时,dropdownFor selectedValue总是为空?

2 个答案:

答案 0 :(得分:1)

在您需要使用jquery的操作链接中包含选定的值。所以在你看来改变

@Html.ActionLink("Run Report", "ViewReport", "Report",new { reportID = Model.pkReportID}, null)

<a class="linkButton" href="#">Report</a>

然后在你的javascript中

$('#pkReportID').on('change', function(){
    var url = '@Url.Action("Run Report", "ViewReport",new { reportID = "----"})'.replace("----", $('#pkReportID').val());
    $('.linkButton').attr('href', url);
});

这将根据下拉列表中的所选项目更改链接的href。如果您有任何疑问,请告诉我。

答案 1 :(得分:0)

好的想通了。我做的是在我的控制器中我填充了下拉列表。在我的viewModel中,我需要传递给下一个视图的所有属性。

这是我的控制器代码

  public ActionResult Index()
        {

           var reportList =  PopulateReports();
           var reportViewModel = new ReportsViewModel { ReportList = reportList };


            return View("Index", reportViewModel);
        }


        /// <summary>
        /// Start method
        /// </summary>
        /// <returns></returns>
        public ActionResult Start()
        {
            return Index();
        }

    /// <summary>
    ///  Returns View ReportViewer.cshtml
    /// </summary>
    /// <returns></returns>
        //public ActionResult ViewReport(int reportID, string startDate)
        public ActionResult ViewReport(ReportsViewModel reportsViewModel)
        {

            //ViewData["ArReport"] =
        var report = (ReportsViewModel.ReportNamesList) 0;
        int reportID = int.Parse(reportsViewModel.SelectedReport);
        if (Enum.IsDefined(typeof(ReportsViewModel.ReportNamesList), reportID))
        {
            report = ((ReportsViewModel.ReportNamesList)reportID);

        }



            var arReport = GenerateReport.RunReport(report);
            ViewBag.ArReport = arReport;
            return View("ViewReport", reportsViewModel);
        }


  private SelectList PopulateReports()
        {


            var reports = db.Reports.Where(r => r.IsActive).OrderBy(r => r.ReportOrder).ToList();
            Reports selectedReport = null;
            if (reports.Any())
            {
                selectedReport = reports[0];
            }
            var reportList = new SelectList(reports, "pkReportID", "ReportName", selectedReport);
            //, SelectedReport);// db.Reports.Where(r => r.IsActive).OrderBy(r => r.ReportOrder).ToList();

            return reportList;

        }

这是我的ViewModel

 public class ReportsViewModel
    {
        #region Declaration/Initialization

        private SchedulingEntities db = new SchedulingEntities();

        public enum ReportNamesList
        {
            test1= 1,
            test2= 2,
        }
        #endregion Declaration/Initialization

        #region Properties

        public string FilterEndDate { get; set; }

        /// <summary>
        /// Holds the values of the reports used to populate controls like the dropdownlist.
        /// </summary>
        public SelectList ReportList { get; set; }

        /// <summary>
        /// Holds the selected value of the dropdownlist control.  Needs to be a string or it doesnt work.
        /// </summary>
        public string SelectedReport { get; set; }

        public string FilterStartDate { get; set; }
        #endregion Properties

        #region Constructor

        public ReportsViewModel()
        {
        }

        #endregion Constructor
    }

这是我的观点

@model Scheduling.ViewModel.ReportsViewModel
@Styles.Render("~/Content/themes/base/minified/jquery-ui.min.css")
@Scripts.Render("~/Scripts/Reports.js")
@{
    ViewBag.Title = "Index";
}

<br />
<br />
<br />
<br />
<br />
<br />
<br />

@using (Html.BeginForm("ViewReport","Report", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Reports</legend>
        <div class="editor-field">
          @Html.DropDownListFor(model => model.SelectedReport, Model.ReportList, new { id = "ui_ddlReportsList", style = "width: 25%;" })
        </div>
        <div id ="ui_div_BroadcastReportFilters">
           @Html.Label("Start Date:")
            @Html.EditorFor(model => model.FilterStartDate, new{id = "ui_txt_StartDate" })
             @Html.Label("End Date:")
            @Html.EditorFor(model => model.FilterEndDate, new{id = "ui_txt_EndDate" })

        </div>
        <p>
            <input id="ui_btn_RunReport" type="submit" value="Run Report" />  

        </p>

    </fieldset>
}