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.
- What am I doing wrong ?
- Do I need a model to post the data edited back to the server ?
- What should the correct model look like ?
Thanks