-1

i have a dropdown as seen below, trying to pass my id to controller but always comes as 0

 <div class="form-group">
        @Html.LabelFor(model => model.SagsTypersId, "SagsTypeId1", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("SagsTypeId1", null, htmlAttributes: new {
           @class = "selectpicker",
           data_show_subtext = "true",
           data_live_search = "true"
       })
            @Html.ValidationMessageFor(model => model.SagsTypersId, "", new { @class = "text-danger" })
        </div>
    </div>

heres is my controller Get.

 public ActionResult Create()
    {

        ViewBag.SagsTypeId1 = new SelectList(db.SagsTypers, "Id", "SagsTyper");

        return View();
    }

here is controller Post

 public ActionResult Create(Sag sag)
    {

        if (ModelState.IsValid)
        {
            db.Sags.Add(sag);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        return View(sag);
    }

to sum it up, my issue is that the Sags.SagsTypersId is 0 when trying to submit. Can't seem to locate the error.

Best regards Johnny.

  • `SagsTypersId` is not equal to `SagsTypeId1`! (use the strongly typed `DropDownListFor()` method - `@Html.DropDownListFor(m => m.SagsTypersId, (IEnumerable)@ViewBag.SagsTypeId1)` –  Mar 23 '17 at 11:29
  • oka i will try. – Johnny Tyrac Mar 23 '17 at 11:30
  • And then get rid of your `ViewBag` and do it correctly using a view model (refer [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o)) –  Mar 23 '17 at 11:33

2 Answers2

0

You did not attach the dropdown list to the model, use @Html.DropDownListFor instead of @Html.DropDownList

where SagsTypersList is list contains SagsTypersID & SagsTyperName

@Html.DropDownListFor(model => model.SagsTypersId, 
          new SelectList(SagsTypersList, "SagsTypersID", "SagsTyperName"), htmlAttributes: new {
           @class = "selectpicker",
           data_show_subtext = "true",
           data_live_search = "true"
       })
NikhilGoud
  • 574
  • 1
  • 5
  • 21
0

Change

  @Html.DropDownList("SagsTypeId1", null, htmlAttributes: new {
           @class = "selectpicker",
           data_show_subtext = "true",
           data_live_search = "true"
       })

to

  @Html.DropDownList("SagsTypeId", null, htmlAttributes: new {
           @class = "selectpicker",
           data_show_subtext = "true",
           data_live_search = "true"
       })

OR Simply use strongly typed html helper:

@Html.DropDownListFor(model => model.SagsTypersId, 
          new SelectList(SagsTypersList, "SagsTypersID", "SagsTyperName"), htmlAttributes: new {
           @class = "selectpicker",
           data_show_subtext = "true",
           data_live_search = "true"
       })
Jaimin Dave
  • 1,224
  • 10
  • 18
  • Thanks i ended up solving it like this: @Html.DropDownListFor(model => model.SagsTypersId, (IEnumerable)@ViewBag.SagsTypeId1, htmlAttributes: new { @class = "selectpicker", data_show_subtext = "true", data_live_search = "true" }) – Johnny Tyrac Mar 23 '17 at 11:59