2

I am trying to create a simple reporting page with a static number of dropdown lists containing the names of the available reports and add them to the collection of reports for the generation of the comparison report.

Here is my ViewModel

public class DealSummaryComparisonViewModel
{
    public ICollection<DealSummary> AvailableDealSummaries { get; set; }
    public ICollection<int> SelectedDealSummaries { get; set; }
}

and here is my View

@model FRSDashboard.Web.Models.DealSummaryComparisonViewModel

@{
     ViewBag.Title = "FRS Dashboard :: Deal Summary Comparison Report";
     var dealSummaryList = new SelectList(Model.AvailableDealSummaries, 
                                          "DealSummaryId", "FileName");
}

<h2>Deal Summary Comparison Report</h2>

<p>Please select at least two files to compare:</p>

@using (Html.BeginForm("DealSummaryComparison", "Reporting", FormMethod.Post)) {
     for (int i = 0; i <= 4; i++) {
         <div>File @i+1: @Html.DropDownListFor(m => m.SelectedDealSummaries,
                                               dealSummaryList)</div>
     }

<div><input type="submit" name="download" id="download" value="Download To Excel" /></div>
}

This works for populating the dropdown lists but I'm not sure how to populate the SelectedDealSummaries collection or remove a selected deal summary from the list (similar to what I would do with WebForms).

Also, how would I add an item to the list like "Select Deal Summary..."?

UPDATE:

I have modified my code to use indexing and call a javascript function.. but am stuck on what to do in javascript.

@using (Html.BeginForm("DealSummaryComparison", "Reporting", FormMethod.Post)) {
    for (int i = 0; i <= 4; i++) {
        <div>
            File @(i + 1): 
            @Html.DropDownListFor(m => m.SelectedDealSummaries[i], dealSummaryList, 
                new {onchange = "updateAvailable()"})
        </div>
     }
}

<script type="text/javascript">

     function updateAvailable() {
          alert("updating");
     }

</script>

1 Answer 1

1

You need to use indexes on your collection -> m => m.SelectedDealSummaries[i]

@using (Html.BeginForm("DealSummaryComparison", "Reporting", FormMethod.Post)) {
     for (int i = 0; i <= 4; i++) {
         <div>File @i+1: @Html.DropDownListFor(m => m.SelectedDealSummaries[i],
                                               dealSummaryList)</div>
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I have altered my code to do this, and it works great. I have also added a call to a javascript function to update the list but am stuck as to how to modify the objects in the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.