-1

My issue: the dropdownlistfor is not selecting the correct value of string when loading the page. Here is the code and view:

Surcharges.html

@model IEnumerable<IEnumerable<string>>
@{
   ViewBag.Title = "Surcharge Management";
}
<div id="mainTitleDiv" class="mainTitle">
    <span style="display:inline-block;">@ViewBag.Title</span>
</div>
<br />
<table class="table">  
@{
    int i = 0;
    foreach (var surcharge in Model)
    {
        int j = 0;
        <tr>
            @foreach (var item in surcharge)
            {
                if (i == 0)
                {
                    @:<th>@Html.DisplayFor(model => item)</th>
                }
                else
                {
                    if (j == 0)
                    {
                        @:<th>@Html.DisplayFor(model => item)</th>
                    }
                    else
                    {
                        @:<td>
                        if (!string.IsNullOrEmpty(item) && (i == 2)) // Type
                        {
                            @Html.DropDownListFor(x => item, new List<SelectListItem>
                            {
                                new SelectListItem() {Text = "Fixed Amount", Value ="0"},
                                new SelectListItem() {Text = "Percentage",  Value ="1"}
                            }, new { style = "width:100%; height:34px;" } )
                        }
                        else
                        {
                            @Html.EditorFor(model => item, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => item, "", new { @class = "text-danger" })
                        }
                        @:</td>
                    }
                    j++;
                }
            }
        </tr>
        i++;
    }
}

AdminController.cs

public ActionResult Surcharges()
{
    if (!User.Identity.IsAuthenticated)
    {
        return RedirectToAction("Login", "Account");
    }

    int userId = User.Identity.GetUserId<int>();
    var query = db.Surcharges.Where(x => x.UserId == userId).OrderBy(x => x.Id).ToList();
    IEnumerable<IEnumerable<string>> surcharges = Utility.Pivot<string>(Utility.GetSurchargeList(query));            

    return View(surcharges);
}

Surcharges page Surcharge

S2 and D1 have the string value "1" and not selected by the dropdownlist.

  1. What am I doing wrong ?
  2. Do I need a model to post the data edited back to the server ?
  3. What should the correct model look like ?

Thanks

mcentro
  • 1
  • 1
  • You cannot use a `foreach` loop to generate form controls for a collection - binding does not work (refer [Post an HTML Table to ADO.NET DataTable](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) –  Sep 22 '17 at 00:42
  • You also need to refer [MVC5 - How to set “selectedValue” in DropDownListFor Html helper](https://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper/41731685#41731685) for dealing with a dropdownlist in a loop –  Sep 22 '17 at 00:43

1 Answers1

0

Thanks, Stephen. My ASP.NET MVC project requirement is to design a page that looks like this image:

Surcharge Page

and my Surcharge table and data look like this:

Surcharge Table

What is your suggestion regarding the model that properly works and binds to the table ?

Is it better to use a grid with Edit and Delete buttons instead of the foreach table ?

I'm looking for the best model that actually allows each row to be edited and saved into the table (dropdownlist for the Type field).

mcentro
  • 1
  • 1