ADBF
ADBF

Reputation: 93

@html does not exist in the current context

Trying to start with Blazor. I took the Weatherforecast project and tried to add a dropbox with @HTML. All very standard. However I do keep getting the error that @html does not exist in the current context. This question has been asked before, I did see the warnings about similar questions but I did not find a good answer yet. I did add System.Web.MVC to the Imports.razor sheet. Also as added as an assembly. I could not find the dll at nuget, so I got it from this directory: C:\Program Files (x86)\Microsoft VisualStudio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\Web\Razor\v3.0\MVC5.2.2.

I rebuilded the project, got the latest update for VS 2109. I still just cant seem to access the right dll for @html. Any help would be greatly appriciated.

Arnold

''' @page "/fetchdata" @using VAblazor.Data @using System.Web.Mvc.Html @using System.Web.Mvc

@inject VAEntryService VAService

//the dropdown wont work offcourse, it is just that this leads to the error

@Html.DropDownList("StudentGender",
                    new SelectList(Enum.GetValues(typeof(Gender))),
                    "Select Gender",
                    new { @class = "form-control" }) 

'''

Upvotes: 0

Views: 2937

Answers (1)

enet
enet

Reputation: 45764

You can't use Html Helpers in Blazor... You should learn the Component model...how to create components, and how to embed inside them html elements such as select, button, etc.

In addition to using the common html elements, you can use Blazor Forms components that represent those html elements, such as InputText and InputSelect.

As per request, the following code creates a component with a select element:

countries.razor

@page "/countries"

@if (countries == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <select id="countries" onchange="@CountrySelectionChanged">
        <option></option>
        @foreach (var country in countries)
        {
            <option value="@country.ID">@country.Name</option>
        }
    </select>
}

@code
{
   // Here you should have code that retrieve a list of counties from a data store.
}

Upvotes: 1

Related Questions