我正在使用Visual Studio 2017在NET Core 2.1.1中运行项目,并且遇到以下问题:
视图未生成相应的HTML。
我有以下控制器:
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");
}
}
我有关联的视图:
@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>
它生成的HTML如下,取自浏览器,并且无法添加国家/地区:
<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)
标签助手已加入,需要您导入它们才能使用。
确保您的Views文件夹中有一个名为_ViewImports.cshtml的文件,并确保它添加了所需的帮助程序的命名空间。
例如,对于所有默认的MS帮助器,请使用以下命令:
@using AuthoringTagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, AuthoringTagHelpers