首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >asp.net mvc wcf服务:没有为此对象错误定义无参数构造函数。

asp.net mvc wcf服务:没有为此对象错误定义无参数构造函数。
EN

Stack Overflow用户
提问于 2017-07-24 14:08:57
回答 1查看 161关注 0票数 0

正在开发一个具有基础设施、仓库、服务、网络的ASP.NET MVC应用程序。当我运行我的视图(索引、创建等)时,我有以下错误:

无参数图像

见下面的代码

控制器

代码语言:javascript
复制
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);
    }
}

}

索引视图

代码语言:javascript
复制
@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")

请告诉我错过了什么地方。

EN

回答 1

Stack Overflow用户

发布于 2017-07-25 10:17:03

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45282756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档