0

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 ?

5
  • I think a_NEquipe needs to be declared as public IEnumerable<SelectListItem> in the model Commented Jul 11, 2019 at 8:24
  • In all posts or tutorials I found about this subject, the variable where the selected value would be stored was always an int or a string. Anyway I tried to declare a_NEquipe as a IEnumerable<SelectListItem> but it still empty Commented Jul 11, 2019 at 8:34
  • in your view put @Html.DropDownListFor(m => m.a_NEquipe, Model.EquipesTavaux), in the model IEnumerable<SelectListItem> a_NEQuipe, string EquipesTavaux Commented Jul 11, 2019 at 8:37
  • Try changing a_NEquipe from a string to an int as this is the indexer for the dropdown list Commented Jul 11, 2019 at 9:05
  • @JamesS I tried to use an int too, I had to make it nullable (int?) to avoid the automation of the [Required] tag when you use a non-nullable type in your model. Actually, even if I use a string or an int, my variable still empty Commented Jul 11, 2019 at 9:08

1 Answer 1

1

Try to specify the Text and Value fields like this:

@Html.DropDownListFor(model => model.a_NEquipe, Model.EquipesTravaux.Select(x => new SelectListItem { Text = x.TRAV_SEQ, Value = x.TRAV_CODE }), new { @class = "form-control"})

Sign up to request clarification or add additional context in comments.

6 Comments

I tried your solution like this : @Html.DropDownListFor(model => model.a_NEquipe, Model.EquipesTravaux.Select(x => new SelectListItem { Value = x.TRAV_SEQ + "", Text = x.TRAV_CODE }), new { @class = "form-control" }). The Dropdown still being populated as expected but the model.a_NEquipe still null...
Can you write a javascript function to display the selected value in an alert box? Then we will know if the selected item is right.
Yes I had the same idea ! I added this piece of code : document.getElementById('a_NEquipe').addEventListener('change', function () { var e = document.getElementById('a_NEquipe') alert(e.options[e.selectedIndex].text); }); And the correct value is displayed in the alert, it appears that the value is lost during the postback sending
Can you try e.options[e.selectedIndex].value instead of e.options[e.selectedIndex].text?
Dont be sorry, that is what stackoverflow is for. If the answer helped I hope you dont mind accepting and up-voting the answer :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.