2

Dynamically i should load dropdown list and display the selected value. but dropdown loaded sucessfully but default value not selected.

@gt.PlantId - integer, PlantId -integer

        @foreach (var gt in Model.RoleList)
        {
            <tr>
                <td>@Html.DropDownListFor(Model => Model.Plants,new SelectList(Model.Plants,"PlantId","PlantName", @gt.PlantId))</td>
                <td>@gt.PlantId</td>
                <td>@gt.RoleId</td>
                @using (Ajax.BeginForm("deletedet", new AjaxOptions() { UpdateTargetId = "Edit-User", AllowCache = true, InsertionMode = InsertionMode.ReplaceWith }))
                {
                    @Html.Hidden("userId", @gt.UserId)

                <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#myTable"><span class="glyphicon glyphicon-trash"></span></button></p></td>
                }
            </tr>

        }
6
  • @Html.DropDownListFor(m => m.Genre, new SelectList(Model.Genres, "Id", "Name"), "DEFAULT VALUE GOES HERE", new { @class = "form-control" }) Commented May 13, 2017 at 20:29
  • Try above and make changes accordingly. "DEAFULT VALUE GOES HERE" Replace it with what you want as default Commented May 13, 2017 at 20:31
  • Hi Unbreakable, Thank you very much. based on value i have to display the text. when i enter "DEFAULT VALUE GOES HERE" in this my id i'm getting error like cant convert into to string Commented May 13, 2017 at 20:54
  • <td>@Html.DropDownListFor(Model => Model.Plants,new SelectList(Model.Plants,"PlantId","PlantName", Model.Plants))</td> Commented May 13, 2017 at 21:23
  • Did it work...? Commented May 13, 2017 at 21:28

1 Answer 1

1

If foreach loop @Html.DropDownListFor ,@Html.HidenFor, @Html.TextBoxFor or any other input element never working, because in razor input/select/textarea create a unique html name/id attribute. But using foreach it can't do this. so use for loop or EditorTemplates rather than foreach.

Other wise you can generate the html but you can't send list of item in your action.

Example:

Model:

public class UserEditorViewModel
    {
       public string UserId { get; set; }
        public string RoleId { get; set; }
        public string UserName { get; set; }
        public IEnumerable<Roles> Roles { get; set; }
    }

EditorTemplates need to exist in either Views/Shared/EditorTemplates or Views/ControllerName/EditorTemplates and name of the view (by default) should be the name of the object(Or Name of the Model) you want to use it as the template.

Editortemplates:

Views/Shared/EditorTemplates/UserEditorViewModel.cshtml

@model UserEditorViewModel

<div class="form-group">
    @Html.DisplayNameFor(m => m.UserId)
    @Html.EditorFor(m => m.UserId)
</div>
<div class="form-group">
    @Html.DisplayNameFor(m => m.UserName)
    @Html.EditorFor(m => m.UserName)
</div>
<div class="form-group">
    @Html.DisplayNameFor(m => m.RoleId)
    @Html.DropDownListFor(m => m.RoleId, new SelectList(Model.Roles,"RoleId","RoleName",Model.RoleId))
</div>

View :

@model UserEditorViewModel

@using (Html.BeginForm("--Your Action--", "--Your Controller--"))//Or use Ajax.BeginForm if you need
{
    @Html.EditorForModel()
    <input type="submit" value="Save" />
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Friend, Thank you very much. its working fine one more problem is i want to save the all changed dropdownlist value. for example one user it will come multiple roles and departments dropdownlist i want to save all the dropdown values. can you help me please.
Simple your action receive something like ActionName(List<UserEditorViewModel> model) .@SENA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.