2017-07-24 76 views
0

我正在开发一个带有基础结构,存储库,服务(WCF),Web的ASP.NET MVC应用程序。当我运行我的看法(索引,创建等) 我有这样的错误:asp.net mvc wcf服务:没有为此对象定义的无参数构造函数 - 错误

parameterless image

见下

控制器

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") 

Pleaase我错过了它。

+2

标准误导性讯息。检查你的DI容器,并确保解决'ICountriesService'时没有问题。确保所有的依赖关系都已注册。 – Nkosi

+1

那么如何设置你的依赖注入呢? – CodeCaster

回答

0

您必须告诉asp.net mvc您正在使用DI容器来创建您的对象。注册通常在global.asax应用程序启动事件中完成。

相关问题