The model item passed into the dictionary is of ty

2019-02-25 22:16发布

问题:

I'm newbie to MVC architecture.When I'm trying to update, its showing error ,Its totally strange but the data is updating.

The model item passed into the dictionary is of type 'CMS.Domain.Models.Site', but this dictionary requires a model item of type 'CMS.Web.ViewModels.SiteModel'.'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'CMS.Web.ViewModels.SiteModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[CMS.Web.ViewModels.SiteModel]'.

My code looks like:

ViewModels:

namespace CMS.Web.ViewModels
{
    public class SiteModel
    {

        public SiteModel()
        {
            SiteStatus = new List<SelectListItem>();
        }

        [Key]
        public int ID { get; set; }

        [Required(ErrorMessage = "Site Name is required")]
        [Display(Name = "Site Name")]
        public string Title { get; set; }


        [Display(Name = "Require Login")]
        public bool RequiresLogin { get; set; }

        [Display(Name = "Force HTTPS")]
        public bool ForceHTTPS { get; set; }

        [Display(Name = "Enable Approval")]
        public bool Approval { get; set; }

        [AllowHtml]
        public IList<SelectListItem> SiteStatus { get; set; }

        public bool Deleted { get; set; }

        public string CreatedBy { get; set; }

        public DateTime CreatedOn
        {
            get { return _createdOn; }
            set { _createdOn = value; }
        }
        private DateTime _createdOn = DateTime.Now;

        public string LastUpdatedBy { get; set; }

        public DateTime LastUpdatedOn  
        {
            get { return _lastUpdatedOn; }
            set { _lastUpdatedOn = value; }
        }
        private DateTime _lastUpdatedOn = DateTime.Now;

        [Display(Name = "Site State")]
        public string SiteState { get; set; }

    }
}

Model:

namespace CMS.Domain.Models
{
    public partial class Site : Model
    {

        public string Title { get; set; }
        public bool Approval { get; set; }
        public bool RequiresLogin { get; set; }
        public bool ForceHTTPS { get; set; }
        public virtual string SiteStatus { get; set; }
        public bool Deleted { get; set; }

    }
}

Controller:

 public ActionResult Index()
        {
            var _sites = _siterepository.FindAll();
            return View(_sites);
        }

        public ActionResult Add()
        {
            var model = new SiteModel();
            var _SiteStatus = _siterepository.GetSiteStatus();
            foreach (var _sitestatus in _SiteStatus)
            {
                model.SiteStatus.Add(new SelectListItem()
                { 
                    Text = _sitestatus.StatusName, 
                    Value = _sitestatus.StatusName.ToString()
                });

            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Add(SiteModel _sitemodel)
        {
            var model = _sitemodel.ToEntity();
            _siterepository.Add(model);
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var model = new SiteModel();
            var Site = _siterepository.Find(id);
            model = Site.ToModel();
            var _SiteStatus = _siterepository.GetSiteStatus();

            foreach (var _sitestatus in _SiteStatus)
            {
                model.SiteStatus.Add(new SelectListItem()
                {
                    Text = _sitestatus.StatusName,
                    Value = _sitestatus.StatusName.ToString(),
                    Selected = _sitestatus.StatusName == Site.SiteStatus
                });

            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(SiteModel _sitemodel)
        {
            var model = _sitemodel.ToEntity();
            _siterepository.Update(model);
            return View(model);
        }

I'm struggling to resolve this , please help.

回答1:

Check your View's model declaration. It is expecting an enumerable list (IEnumerable<CMS.Web.ViewModels.SiteModel>), but you are passing it a single instance of CMS.Web.ViewModels.SiteModel