none
DropDownListFor In Table Column RRS feed

  • Question

  • I am working on an MVC project and am having some issues in getting the dropdownlistfor to work within a table dynamically created by a foreach loop.

    Reports.cshtml (View)

    @model ClarkePortal.Models.

    ReportsViewModel

    @{Layout = "~/Views/Shared/_Layout.cshtml";}

    <div id="page-wrapper">
     <div class="row">
     <div class="col-lg-12">
      <h1 class="page-header">Reports</h1>
     </div>
    <!-- /.col-lg-12 -->
     </div>
     <div class="row">
     <div class="col-lg-12">
                @if (ViewContext.ViewData.ModelState.ContainsKey(string.Empty))
                {

                    @Html.ValidationSummary(true, null, new { @class = "alert alert-danger" })
                }

                @using (Html.BeginForm("Report", "Portal", FormMethod.Post, new { role = "form" }))

                {

                    @Html.AntiForgeryToken()
      <div class="table-responsive">
      <table class="table table-striped table-bordered table-hover" id="dataTables-example">
       <thead>
        <tr>
         <th>Date</th>
         <th>Service</th>
         <th>Description</th>
         <th>Report</th>
        </tr>
       </thead>
      <tbody>
                                @foreach (var ServiceOrder in Model.ServiceOrders)
                                {

        <tr>
         <td>@((ServiceOrder.ServiceDate != null) ? ((DateTime)ServiceOrder.ServiceDate).ToString("MMM-dd-yyyy") : "N/A")</td>
         <td>@ServiceOrder.ExternalDescription</td>
         <td>@ServiceOrder.ServiceKit</td>
         <td>@Html.DropDownListFor(m => m.SelectedReportUrl, new SelectList(Model.ReportTypesDictionary[(int)ServiceOrder.ServiceOrderPK], "ReportUrl", "Description"), "Select a report...", new { @class = "form-control", @onchange = "this.form.submit();" })</td>
        </tr>
                                }
      </tbody>
      </table>
     </div>
     <!-- /.table-responsive -->
                }
    </div>
    <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    </div>
    <!-- /#page-wrapper -->

    ReportsViewModel
    public class ReportsViewModel
        {
     public List<ServiceOrderViewModel> ServiceOrders { get; set; }
     public Dictionary<int, List<ReportTypeViewModel>> ReportTypesDictionary { get; set; }
     public string SelectedReportUrl { get; set; }
        }

    ReportViewModel
    public class ReportViewModel
        {
     public string ReportUrl { get; set; }
        }

    Reports Controller       
    public ActionResult Reports()
            {
     UserCustomer selectedUserCustomer = GetNavigationContext(User.Identity.GetUserId());
     ReportsViewModel reportsViewModel = new ReportsViewModel();

     Dictionary<int, List<ReportTypeViewModel>> reportTypesDictionary = new Dictionary<int, List<ReportTypeViewModel>>();
     
     List<ServiceOrderViewModel> serviceOrderViewModels = new List<ServiceOrderViewModel>();
     List<ServiceOrder> serviceOrders = ServiceOrder.Find(null, selectedUserCustomer.CustomerPK, null);  //9165 (B01220 Bloomingdale), 6668 (D00100 Darien)
     int i = 1;
     foreach (ServiceOrder serviceOrder in serviceOrders)
                {
                    serviceOrderViewModels.Add(new ServiceOrderViewModel(serviceOrder));

      List<AppObject> appObjects = AppObject.Find(null, null, serviceOrder.WorkTypePK, null, true);
      List<ReportTypeViewModel> reportTypes = new List<ReportTypeViewModel>();
      foreach (AppObject appObject in appObjects) {
       string reportUrl = appObject.NavigateURL + "&UserName=crusso&ServiceOrderPK=" + serviceOrder.ServiceOrderPK;
                         reportTypes.Add(new ReportTypeViewModel() { ReportUrl = reportUrl, Description = appObject.Description });
                        i++;
                    }

                    reportTypesDictionary.Add((int)serviceOrder.ServiceOrderPK, reportTypes);
               }
                reportsViewModel.ServiceOrders = serviceOrderViewModels.OrderByDescending(o => o.ServiceDate).ToList();
                reportsViewModel.ReportTypesDictionary = reportTypesDictionary;
      return View(reportsViewModel);

            }

            [HttpPost]
            [ValidateAntiForgeryToken]

     public ActionResult Report(ReportsViewModel reportsViewModel)
            {

      ReportViewModel reportViewModel = new ReportViewModel();
      var rptInfo = new ReportInfo
                 {
                     ReportDescription = "Report",
                     ReportName = "Report",
                     ReportURL = String.Format("../../Reports/ReportTemplate.aspx?ReportName={0}&Height={1}", reportsViewModel.SelectedReportUrl, "50%"),
                     Width = 99,
                     Height = 50
                 };
                 ViewBag.rptInfo = rptInfo;
                 reportViewModel.ReportUrl =
      string.Format("../../Reports/ReportTemplate.aspx?ReportName={0}", reportsViewModel.SelectedReportUrl);
      return View(reportViewModel);

            }

    The table gets created correctly with the drop-downlist containing the right values for the SelectedReportUrl.
    Below is a sample of what the generated page looks like.
                <form action="/Portal/Report" method="post" role="form"><input name="__RequestVerificationToken" type="hidden" value="sbIwwG_aGN4B-nBR456-LfS8pwg53HsssJ56k8jPbiQ_tLlrJ1oGxelMAu8Wq-fp2ma_leSCWvpIElYLq3Gh6jmMlS-bqXQjhZaeYsXB8yIxxl2kEuP-YlkaIkBCZlmqV0KsHoonpO8-xXm_RNvSDQ2" />                <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Service</th>
                                    <th>Description</th>
                                    <th>Report</th>
                                </tr>
                            </thead>
                            <tbody>
                                    <tr>
                                        <td>Jul-14-2017</td>
                                        <td>Aquatic Weed and/or Algae Treatment</td>
                                        <td>Aquatic Vegetation Control</td>
                                        <td><select class="form-control" id="SelectedReportUrl" name="SelectedReportUrl" onchange="this.form.submit();"><option value="">Select a report...</option>
    <option value="Aquatic">http://xxx.xxx.azure.com:80/Reportserver?/PriusReportsDev/Aquatic+Weed+Report+Master&amp;UserName=crusso&amp;ServiceOrderPK=238259">Aquatic Weed Report</option>
    </select></td>
                                    </tr>
                                    <tr>
                                        <td>Dec-28-2016</td>
                                        <td>Aquatic Weed and/or Algae Treatment</td>
                                        <td>Aquatic Vegetation Control</td>
                                        <td><select class="form-control" id="SelectedReportUrl" name="SelectedReportUrl" onchange="this.form.submit();"><option value="">Select a report...</option>
    <option value="Aquatic">http://xxx.xxx.azure.com:80/Reportserver?/PriusReportsDev/Aquatic+Weed+Report+Master&amp;UserName=crusso&amp;ServiceOrderPK=236833">Aquatic Weed Report</option>
    </select></td>
                                    </tr>
                                    <tr>
                                        <td>Dec-27-2016</td>
                                        <td>Aquatic Weed and/or Algae Treatment</td>
                                        <td>Aquatic Vegetation Control</td>
                                        <td><select class="form-control" id="SelectedReportUrl" name="SelectedReportUrl" onchange="this.form.submit();"><option value="">Select a report...</option>
    <option value="Aquatic">http://xxx.xxx.azure.com:80/Reportserver?/PriusReportsDev/Aquatic+Weed+Report+Master&amp;UserName=crusso&amp;ServiceOrderPK=236821">Aquatic Weed Report</option>
    </select></td>
                                    </tr>

    However, I only get the SelectedReportUrl back when I select the value out of the first drop-down box.  Otherwise, I get NULL.
    Any help would be greatly appreciated as this is a time sensitive matter.  If you need any further explanation please let me know.

    Tuesday, August 22, 2017 9:48 PM

All replies

  • Perhaps asking here is a better place to ask your question which is a dedicated forum for ASP.NET

    https://forums.asp.net/


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, August 22, 2017 10:23 PM
  • Hi crusso1,

    According to your question is more related to ASP.NET, you could post a new thread to ASP.NET forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools . If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution .

    Best Regards,
    Feih-7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Thursday, August 24, 2017 9:18 AM
    Thursday, August 24, 2017 9:17 AM