1

I am working with an Edit page that have some drop down list and a model. I want to set the seleted value for the dropdown list from viewbag. But cannot do it. Can you suggest any other way to do it.

In the controller I have done like this

ViewBag.TraineeId = _employeeService.GetEmployeeNamelist(SmartSession.CompanyId);

View

<div id="AttributeDetails">
    @foreach (var item in Model.TraineeAssignDetail)
    { 
        @Html.Partial("~/Areas/HRM/Views/TraineeAssign/Partial.cshtml", item)
    }
</div>

I am passing the model item through in the partial view using @Html.Partial()

<div class="col-md-2 col-sm-2 col-xs-6">
    @Html.DropDownList("TraineeId", new SelectList(ViewBag.TraineeId, "Id", "FullName", Model.TraineeId), "--Select--", new { @class = "form-control col-md-7 col-xs-12 default ddlErrorMsg", @id = "ddlMaterialGroupId" })
    @Html.ValidationMessageFor(model => model.TraineeId)
</div>

Please show me some way to do it.enter image description here

  • You cannot use the same name to the property your binding to and the `SelectList` (refer [this answer](http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557)). And you cannot use `foreach` loops or partials to generate form controls for collections (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) –  Oct 30 '16 at 05:37
  • Since your wanting to use a dropdownlist in a loop - refer [this answer](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array/37411482#37411482) for options –  Oct 30 '16 at 05:38
  • Thank you very much for the guidance. – momtahinul isdani Oct 30 '16 at 05:54

0 Answers0