3

I modified the default login of asp.net mvc 6 and wanted to add a dropdown-list with taghelper select but it seems like it produces a wrong output.

I added the select-taghelper-element so it looks like this:

<form asp-controller="Account" asp-action="Register" method="post" class="form-horizontal" role="form">
    <div class="form-group">
        <label asp-for="Tenant" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <select asp-for="Tenant" asp-items="ViewBag.Tenants" class="form-control" />
            <span asp-validation-for="Tenant" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <button type="submit" class="btn btn-default">Register</button>
        </div>
    </div>
</form>

after adding the select-tag, the submit-button is not displayed anymore - the dropdown is displayed but contains no data, but using breakpoints Viewbag.Tenants contains data. When I remove the first div with the select-tag, the button is showing.

Any idea what here is wrong?

2
  • have you tried with an @ before ViewBag? Commented Apr 7, 2016 at 18:26
  • @JoeAudette thanks, tried, makes no difference.. Commented Apr 7, 2016 at 18:32

1 Answer 1

2

Your markup is wrong. Do not use the self closing select tag. Have an explicit closing tag.

This should work.

<select asp-for="Tenant" asp-items="ViewBag.Tenants" class="form-control"></select>
Sign up to request clarification or add additional context in comments.

5 Comments

Ahh you're a genius! Thanks! :)
I have tried this and weirdly I get the list rendered with multiple="multiple" as a select list rather than a dropdownlist?
@MarkRedman It depends on the type of the property you use for asp-for If it is an array type (int[]/string[]), It will be rendered with multiple attribute
Its a SelectList type, I have tried both SelectList and List<SelectListItem> (not IEnumerable or an array)
I am talking about the property used for asp-for not asp-items. See Select Tag Helper in MVC 6

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.