使用基础架构,存储库,服务(WCF),Web开发ASP.NET MVC应用程序。当我运行我的视图(索引,创建等) 我有这个错误:
请参阅下面的代码
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BPP.CCSP.Admin.Web.BPPCCSPAdminCountriesService;
namespace BPP.CCSP.Admin.Web.Controllers
{
public class CountriesController : Controller
{
private readonly ICountriesService _countryService;
public CountriesController(ICountriesService countryService)
{
_countryService = countryService;
}
//
// GET: /Countries/
public ActionResult Index()
{
IEnumerable<COUNTRIES> countries = _countryService.GetCountries();
return View(countries);
}
//
// GET: /Countries/Details/5
public ActionResult Details(Int32 id)
{
COUNTRIES country = _countryService.GetCountry(id);
return View(country);
}
//
// GET: /Countries/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Countries/Create
[HttpPost]
public ActionResult Create(COUNTRIES countries)
{
try
{
// TODO: Add insert logic here
if (ModelState.IsValid)
{
_countryService.AddCountry(countries);
return RedirectToAction("Index");
}
}
catch
{
ModelState.AddModelError("", "We cannot add this country. Verify your data entries !");
}
return View(countries);
}
//
// GET: /Product/Edit/5
public ActionResult Edit(int id)
{
COUNTRIES country = _countryService.GetCountry(id);
return View(country);
}
//
// POST: /Product/Edit/5
[HttpPost]
public ActionResult Edit(int id, COUNTRIES countries)
{
try
{
// TODO: Add update logic here
if (ModelState.IsValid)
{
countries.COUNTRY_ID = id;
_countryService.AddCountry(countries);
return RedirectToAction("Index");
}
}
catch
{
return View();
}
return View();
}
//
// AJAX: /Product/Delete/5
[HttpPost]
public ActionResult Delete(int id)
{
_countryService.RemoveCountry(id);
var results = new
{
DeleteId = id
};
return Json(results);
}
}
}
索引视图
@model IEnumerable<BPP.CCSP.Admin.Web.BPPCCSPAdminCountriesService.COUNTRIES>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ACTION_STATUS)
</th>
<th>
@Html.DisplayNameFor(model => model.COUNTRY_CODE)
</th>
<th>
@Html.DisplayNameFor(model => model.COUNTRY_ID)
</th>
<th>
@Html.DisplayNameFor(model => model.COUNTRY_NAME)
</th>
<th>
@Html.DisplayNameFor(model => model.CREATED_BY)
</th>
<th>
@Html.DisplayNameFor(model => model.CREATED_DATE)
</th>
<th>
@Html.DisplayNameFor(model => model.DELETED_BY)
</th>
<th>
@Html.DisplayNameFor(model => model.DELETED_DATE)
</th>
<th>
@Html.DisplayNameFor(model => model.LAST_UPDATE_BY)
</th>
<th>
@Html.DisplayNameFor(model => model.LAST_UPDATE_DATE)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ACTION_STATUS)
</td>
<td>
@Html.DisplayFor(modelItem => item.COUNTRY_CODE)
</td>
<td>
@Html.DisplayFor(modelItem => item.COUNTRY_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.COUNTRY_NAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.CREATED_BY)
</td>
<td>
@Html.DisplayFor(modelItem => item.CREATED_DATE)
</td>
<td>
@Html.DisplayFor(modelItem => item.DELETED_BY)
</td>
<td>
@Html.DisplayFor(modelItem => item.DELETED_DATE)
</td>
<td>
@Html.DisplayFor(modelItem => item.LAST_UPDATE_BY)
</td>
<td>
@Html.DisplayFor(modelItem => item.LAST_UPDATE_DATE)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
@Html.ActionLink("Back to Index", "Index", "Home")
请在哪里错过了。
答案 0 :(得分:0)
您必须告诉asp.net mvc您正在使用DI容器来创建对象。注册通常在global.asax app start event中完成。