I am building a IEnumerable in my HTTPGet and passing it to my view. My issue is getting the selected info back to the controller using a hiddenfor.
[HttpGet]
public ActionResult Index(int id)
{
RANKDModel model = new RANKDModel();
model.thingList = new List<thingsModel>();
List<SelectListItem> thingRank = new List<SelectListItem>();
var tblThingGroup = db.tblThingGroup.Where(x => x.thingCollectionID == id).Include(t => t.tblThing).Include(t => t.tblThingCollection).OrderBy(x => x.displayOrder);
model.thingRank = new SelectList(thingRank, "Value", "Text");
int i = 1;
foreach (var thing in tblThingGroup)
{
thingRank.Add(new SelectListItem()
{
Value = thing.tblThing.thingID.ToString(),
Text = i.ToString()
});
model.thingRank = new SelectList(thingRank, "Value", "Text");
thingsModel things = new thingsModel();
things.thingID = thing.tblThing.thingID;
things.thingName = thing.tblThing.thingName;
things.thingImgPointer = thing.tblThing.thingImgPointer;
model.thingList.Add(things);
i++;
}
return View(model);
}
[HttpPost]
public ActionResult Index(RANKDModel model, IEnumerable<SelectListItem> thingRank)
{
//check model.Selecteditem
return View(model);
}
My view displays the drop downs but I cannot seem to get the values passed back to the controller on Post. Below is my view. The commented lines are failed attempts. I would rather not use ajax here. Since my value and my text fields are all I need, I feel like I should be able to pass them back to the controller even if they are not part of the model (which would be nice but I wasn't successful there either.)
@using (Html.BeginForm())
{
<div class="col-md-8">
@Html.TextBoxFor(modelItem => Model.firstName, new { @class = "form-control" })
@Html.TextBoxFor(modelItem => Model.lastName, new { @class = "form-control" })
</div>
<table class="table">
@Html.HiddenFor(modelItem => Model.thingRank)
@for (int i = 0; i < Model.thingList.Count; i++)
{
string imagePointer = "/Areas/RANKD/thingImages/" + Model.thingList[i].thingImgPointer;
<tr>
<td>
@Html.HiddenFor(modelItem => Model.thingList[i].thingID)
@Html.DropDownListFor(modelItem => modelItem.thingRank, (IEnumerable<SelectListItem>)ViewBag.thingRank, "Rank it Bro.") @*Model.thingRank.Items as IEnumerable<SelectListItem>)*@
@*@Html.HiddenFor(modelItem => modelItem.thingRank[Model.thingRank(i)].Value)*@
@Html.HiddenFor(modelItem => modelItem.thingRank[Model.thingRank.IndexOf(i)].Value)
@*@Html.HiddenFor(modelItem => modelItem.thingRank)*@
@Html.DisplayFor(modelItem => Model.thingList[i].thingName)
</td>
<td>
<img src="@imagePointer" alt="@Model.thingList[i].thingName" height="75" />
</td>
</tr>
}
</table>
<input type="submit" value="RANK IT" class="btn btn-default" />
}
Am I just missing something in my hiddenfor?
Adding the code in my RANDModel
[Serializable]
public class RANKDModel
{
public string firstName { get; set; }
public string lastName { get; set; }
public List<thingsModel> thingList { get; set; }
public IEnumerable<SelectListItem> thingRank { get; set; }
}