ASP.NET MVC5字典项模型传递不起作用

时间:2017-10-13 12:36:23

标签: c# asp.net-mvc

我对 ASP.NET MVC 5 中的应用感到困惑。

我收到此错误:

  

"传入字典的模型项是类型   ' Appli.ViewModel.Account.UserDetails ',但此词典需要   模型项' Appli.ViewModel.Account.ConnectionAccountVM '。“

我不明白,因为在我看来我的模型参考UserDetails。

查看模型

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

        }
    }
}

来自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);
        }

我在视图中的视图&gt;帐户&gt;详情:

@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>

0 个答案:

没有答案