locked
How to pass a selected dropdownlist to edit RRS feed

  • Question

  • User1694748171 posted

    How I can pass the selected item from dropdown list to the edit view, i tried this but it always passed the first item from dropdown list.

    my controller

      ViewBag.AssessmentStandard = new SelectList(db.Exam_ANSI_NASBLA_Standard, "StandardId", "StandardDesc", assessmentStandardId);

    I passed the assessmentStandardId == 2 and it show me the second item in drop down list

    this is my view

       <div>
                        <label>
                            @Html.LabelFor(model => model.AssessmentStandardName, "ANSI/NASBLA Standard", htmlAttributes: new { @class = "required control -label ", @style = "font-weight: bold" })
                            @Html.DropDownListFor(model => model.AssessmentStandardName, new SelectList(ViewBag.AssessmentStandard, "Value", "Text"), new { @class = "form-control" })
                            @*@Html.DropDownListFor(model => model.AssessmentStandardName, null, new { @class = "form-control" })*@
                            @*@Html.EditorFor(model => model.AssessmentStandardName, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })*@
                            @Html.ValidationMessageFor(model => model.AssessmentStandardName, "", new { @class = "text-danger" })
                        </label>
                    </div>

    Tuesday, October 13, 2020 8:20 PM

Answers

  • User1694748171 posted

    I figure out how to do it

      ViewBag.AssessmentStandard = new SelectList(db.Exam_ANSI_NASBLA_Standard.ToList(), "StandardId", "StandardDesc", assessmentStandardId);

    and the view 

       @Html.DropDownList("AssessmentStandard", "Select")

    this is good resource for that 

    http://www.codedigest.com/posts/32/different-ways-of-binding-dropdownlist-in-aspnet-mvc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2020 12:38 AM

All replies

  • User475983607 posted

    A selected value is either selected by the user or stored in a table.  The part of your code that has anything to do with a selected value is the following.

     ViewBag.AssessmentStandard = new SelectList(db.Exam_ANSI_NASBLA_Standard, "StandardId", "StandardDesc", assessmentStandardId);

    Unfortunately, there is no indication how, when or where assessmentStandardId is populated.  Is there anyway you can  explain your design and share relevant code?  

    Also why are there inputs within a label?

    Tuesday, October 13, 2020 8:35 PM
  • User1694748171 posted

    I'm passing the value to dropdownlist so it will show only what the user selected from dropdown list before

     var  var assessmentStandardId = db.Exam_ANSI_NASBLA_Standard.Where(x => x.StandardDesc == assessmentStandardName).Select(x => x.StandardId).FirstOrDefault();= db.Exam_ANSI_NASBLA_Standard.Where(x => x.StandardDesc == assessmentStandardName).Select(x => x.StandardId).FirstOrDefault();

    I'm getting value of 1-6 from assessmentStandardId  each value had different text in dropdownlist. 

    Wednesday, October 14, 2020 12:20 AM
  • User1694748171 posted

    I figure out how to do it

      ViewBag.AssessmentStandard = new SelectList(db.Exam_ANSI_NASBLA_Standard.ToList(), "StandardId", "StandardDesc", assessmentStandardId);

    and the view 

       @Html.DropDownList("AssessmentStandard", "Select")

    this is good resource for that 

    http://www.codedigest.com/posts/32/different-ways-of-binding-dropdownlist-in-aspnet-mvc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2020 12:38 AM