1

I'm running a project in NET Core 2.1.1 with Visual Studio 2017 and I have the following problem:

A view is not generating the corresponding HTML.

I have the following controller:

public class CountryController : Controller {
    IUnitOfWork Context;
    IRepository<Country> countryRepository;

    public CountryController(IUnitOfWork context) {
        countryRepository = context.GetRepository<Country>();
        Context = context;
    }
    public IActionResult Index() {

        CountryViewModel countries = new CountryViewModel(countryRepository.Elements());
        return View(countries);
    }

    public IActionResult Insert (CountryBindingModel country) {
        Country Country = new Country();
        Country.Name = "Canada";
        countryRepository.Insert(Country);
        Context.Save();
        return RedirectToAction("Index");

    }
}

I have the associated view:

@using Domain.Models

@model CountryViewModel

<form asp-controller="Country" asp-action="Insert">
    <label asp-for="Name"></label>
    <input class="form-control" asp-for="Name"/>
    <button type="submit">Add Country</button>
</form>
<ul>
    @foreach (var a in Model.Countries) {
        <li>@(a.Name)</li>;
    }
</ul>

The HTML it generates is the following, taken from the browser, and it does not work to add the country:

<form asp-controller="Country" asp-action="Insert">
    <label asp-for="Name"></label>
    <input class="form-control" asp-for="Name"/>
    <button type="submit">Add Country</button>
</form>
<ul>
</ul>
0

1 Answer 1

3

The tag helpers are opt-in, and require you to import them for them to work.

Make sure you have a file named _ViewImports.cshtml in your Views folder, and make sure it adds the namespaces of helpers that you want.

For example, for all default MS helpers use the following:

@using AuthoringTagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, AuthoringTagHelpers
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.