1

I’m confused with my app in ASP.NET MVC 5.

I get this error:

"The model item passed into the dictionary is of type 'Appli.ViewModel.Account.UserDetails', but this dictionary requires a model item of type 'Appli.ViewModel.Account.ConnectionAccountVM'.”

I don't understand because in my view my model reference UserDetails.

View Model:

using Autofac;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Appli.ViewModel.Account
{
    public class UserDetails : IValidatableObject
    {
        [Required]
        public string Pseudo { get; set; }
        [Required]
        [RegularExpression(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$", ErrorMessageResourceName = "EnterValidEmail", ErrorMessageResourceType = typeof(Resources.Account))]
        public string Email { get; set; }
        [Required]
        [DataType(DataType.Password)]
        [StringLength(500, ErrorMessage = "Votre mot de passe doit contenir au minimum 5 caractères.", MinimumLength = 5)]
        public string Password { get; set; }
        [Required]
        [DataType(DataType.Password)]
        public string PasswordRe { get; set; }
        public DateTime DateNaissance { get; set; }
        [MaxLength(250)]
        public string Adresse { get; set; }
        [MaxLength(10)]
        public string CodePostal { get; set; }
        [MaxLength(50)]
        public string Ville { get; set; }
        [MaxLength(30)]
        public string Pays { get; set; }
        public string Coordonnees { get; set; }
        public bool Details { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {

                if (DateNaissance < new DateTime(1900))
                {
                    yield return new ValidationResult("Date semble incorrect", new[] { "DateNaissance" });
                }
                if (Password != PasswordRe)
                {
                    yield return new ValidationResult("Vos mot de passes ne semblent pas identiques.", new[] { "Password", "PasswordRe" });
                }

        }
    }
}

My method from the Controller :

[Authorize]
        public ActionResult Details(int? id)
        {
           string _pseudo = HttpContext.User.Identity.GetUserId().ToString();
            DomainEntities.User _user = _userData.Get(ident => ident.Pseudo == _pseudo).First();
            var _userDetails = new ViewModel.Account.UserDetails()
            {
                Adresse = _user.Adresse,
                CodePostal = _user.CodePostal,
                Coordonnees = _user.Coordonnees,
                DateNaissance = _user.Date,
                Email = _user.Email,
                Pays = _user.Pays,
                Pseudo = _user.Pseudo,
                Ville = _user.Ville

            };
            return View("Details", model:  _userDetails);
        }

My view in Views > Account > Details :

@model Appli.ViewModel.Account.UserDetails

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Details</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>UserDetails</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Pseudo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Pseudo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Pseudo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>



        <div class="form-group">
            @Html.LabelFor(model => model.PasswordRe, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PasswordRe, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PasswordRe, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DateNaissance, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DateNaissance, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DateNaissance, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Adresse, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Adresse, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Adresse, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CodePostal, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CodePostal, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CodePostal, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Ville, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Ville, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Ville, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Pays, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Pays, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Pays, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Coordonnees, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Coordonnees, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Coordonnees, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Details, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Details)
                    @Html.ValidationMessageFor(model => model.Details, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
Draken
  • 3,134
  • 13
  • 34
  • 54
Devozor92
  • 23
  • 1
  • 5
  • 1
    the code which you have shown above shoudn;t give this error, on which line you are seeing error , and stack trace? – Ehsan Sajjad Oct 13 '17 at 12:45
  • [InvalidOperationException: The model item passed into the dictionary is of type 'Appli.ViewModel.Account.UserDetails', but this dictionary requires a model item of type 'Appli.ViewModel.Account.ConnectionAccountVM'.] – Devozor92 Oct 13 '17 at 13:03
  • System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +175 System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +107 System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +49 System.Web.Mvc.WebViewPage.ConfigurePage(WebPageBase parentPage) +57 System.Web.WebPages.<>c__DisplayClass3.b__2(TextWriter writer) +218 System.Web.WebPages.HelperResult.WSystem.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98 – Devozor92 Oct 13 '17 at 13:04
  • Do you have a custom editor template that is a razor view expecting the ConnectionAccountVM? Or something in _layout? – Jason W Oct 13 '17 at 13:12
  • Try return View("Details", _userDetails); in your controller? I have never seen people using return View("Details", model: _userDetails); – oopsdazie Oct 13 '17 at 15:16
  • 1
    i've already test this, when you write "model : _userDetails" you just set a parameter who was optionnal and it always works for me – Devozor92 Oct 13 '17 at 15:24
  • the solution was i've made a model in my master page (layout) and was in conflict with my user details view. Thanks for your help. ps : you can test "model: _userDetails" it works perfectly peace. – Devozor92 Oct 13 '17 at 15:44

0 Answers0