Similar to this question and a lot of others, I'm not able to retrieve the selected value from my @Html.Dropdownlistfor. But I think I avoided usual errors so I'm not sure what I'm doing wrong. Here is my Controller :
public class AdministrationController : Controller
{
public ActionResult ParamStatique()
{
ParamStatiqueViewModels psvm = new ParamStatiqueViewModels()
{
a_NEquipe = "1"
};
using (Dal dal = new Dal())
{
psvm.EquipesTravaux = dal.GetEquipesTravaux();
}
return View(psvm);
}
[HttpPost]
public ActionResult ParamStatique(ParamStatiqueViewModels psvm)
{
Debug.WriteLine("NEquipe : " + psvm.a_NEquipe);
using (Dal dal = new Dal())
{
psvm.EquipesTravaux = dal.GetEquipesTravaux();
}
return View(psvm);
}
}
My ModelView looks like this :
public class ParamStatiqueViewModels
{
public List<EQUIPE_TRAVAUX> EquipesTravaux { get; set; }
[Display(Name = "N° Équipe")]
public string a_NEquipe { get; set; }
}
Here is my View :
@using (Html.BeginForm("ParamStatique", "Administration", null, FormMethod.Post, new { id = "modalform", role = "form" }))
{
<div class="modal-body" id="modal-body">
<div class="form-group" id="aNEquipe">
@Html.LabelFor(m => m.a_NEquipe, new { @class = "col-form-label" })
@Html.DropDownListFor(m => m.a_NEquipe, new SelectList(Model.EquipesTravaux, "TRAV_SEQ", "TRAV_CODE"), new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.a_NEquipe, "", new { @class = "text-danger" })
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
}
And so my EQUIPE_TRAVAUX class, generated from EF6 :
public partial class EQUIPE_TRAVAUX
{
public short TRAV_SEQ { get; set; }
public string TRAV_CODE { get; set; }
}
The Dropdown is correctly populated and I can select the option I want, but when I click the "Enregistrer" submit button it returns a null a_NEquipe field in my model, even if I correctly specify it in the Html Helper :
@Html.DropDownListFor(m => m.a_NEquipe, new SelectList(Model.EquipesTravaux, "TRAV_SEQ", "TRAV_CODE"), new { @class = "form-control" })
The result of the Debug.WriteLine("NEquipe : " + psvm.a_NEquipe); is always null like this :
NEquipe :
And if I add the [Required] tag to my model, the client-side validation block the form postback call.
How can I retrieve the selected value in the expected variable a_NEquipe ?
a_NEquipefrom a string to an int as this is the indexer for the dropdown list