形式不绑定到正确的对象

本文关键字:对象 绑定 | 更新日期: 2023-09-27 18:32:39

我有类似的对象(我使用的是模型和视图模型,其中视图模型与模型对象具有相同的属性)。

当我编辑一个项目时,我放置了视图模型的编辑器,处理更新/编辑的控制器,他会收到视图模型:

[HttpPost]
public ActionResult Edit(DocumentViewModel model) {
 Mapper.CreateMap < DocumentViewModel, BE.Document > ();
 BE.Document instanceOfDestination = Mapper.Map < BE.Document > (model);
 Container < BE.Document > container = BL.DocumentBL.UpdateDocument(instanceOfDestination);
 if (!container.HasErrors) {
  SetInfo("Saved!");
 } else {
  SetError(container.ErrorMessage);
 }
 return RedirectToAction("Index");
}

问题是此方法永远不会达到,因为模型绑定器构造 BE。文档而不是文档视图模型。

以下是浏览器发送的值:

__RequestVerificationToken:xxx-dontcare
id:36
name:test flash files
documentType.id:5
unit.id:2
reference:FLASH0016
isActive:true
isActive:false
recyclingSpan:1
selectedTopics:1
selectedTopics:2
trainer.id:615952
selectedInstallations:1
selectedInstallations:2
selectedProfessions:3
selectedProfessions:4
selectedProfessions:6

以下是返回 VM 以进行编辑页面的控制器:

[HttpGet]
public ActionResult Edit(int id) {
 var container = BL.DocumentBL.GetAllDocument(new BE.Document() {
  id = id
 });
 if (!container.HasErrors) {
  Mapper.CreateMap < BE.Document, DocumentViewModel > ();
  DocumentViewModel instanceOfDestination = Mapper.Map < DocumentViewModel > (container.Value);
  // fill values for dropdowns and co
  instanceOfDestination.FillPredefinedValuesForUser(GetAdminOrVisibilityUnits());
  return View(instanceOfDestination);
 } else {
  SetError(container.ErrorMessage);
  return RedirectToAction("Index");
 }
}

还有用于文档的模型和视图模型:

文档视图模型:

public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
public string reference { get; set; }
[Required]
[Range(0, 100)]
public int recyclingSpan { get; set; }
[Required]
public bool isActive { get; set; }
[DocumentTypeValidator("DocType is required")] // custom validator
public DocumentType documentType { get; set; }
public PersonnelAM trainer { get; set; }
public List<DocumentVersion> versions { get; set; }
public List<Installation> installations { get; set; }
public List<Profession> professions { get; set; }
public List<Topic> topics { get; set; }
public Unit unit { get; set; }
// not used for edit or create
public PersonnelAM createdBy { get; set; }
public DateTime createdOn { get; set; }
public PersonnelAM editedBy { get; set; }
public DateTime editedOn { get; set; }
// to fill dropdowns
public IEnumerable<SelectListItem> documentTypeSelect { get; set; }
public IEnumerable<SelectListItem> personnelSelect { get; set; }
public IEnumerable<SelectListItem> installationsSelect { get; set; }
public IEnumerable<SelectListItem> professionsSelect { get; set; }
public IEnumerable<SelectListItem> topicTypeSelect { get; set; }
public IEnumerable<SelectListItem> unitSelect { get; set; }

// for multi-selects - uses FillListsFromIds to fill Lists from Ids
public int[] selectedProfessions { get; set; }
public int[] selectedInstallations { get; set; }
public int[] selectedTopics { get; set; }
// For file upload
[MinLengthAttribute(1)]
public HttpPostedFileBase[] files { get; set; }
// for file get
public List<string> filesList { get; set; }

是。公文

public int id { get; set; }
public string name { get; set; }
public string reference { get; set; }
public int recyclingSpan { get; set; }
public bool isActive { get; set; }
public DocumentType documentType { get; set; }
public PersonnelAM trainer { get; set; }
public List<string> filesList { get; set; }
public List<Installation> installations { get; set; }
public List<DocumentVersion> versions { get; set; }
public List<Profession> professions { get; set; }
public List<Topic> topics { get; set; }
public Unit unit { get; set; }
public PersonnelAM createdBy { get; set; }
public DateTime createdOn { get; set; }
public PersonnelAM editedBy { get; set; }
public DateTime editedOn { get; set; }

谢谢帮助我:-)

编辑:

这是完整的 Get/id 控制器

[HttpGet]
        public ActionResult Edit(int id)
        {
            if (User.IsInRole("Admin") || User.IsInRole("Moderator") || SessionManager.matricule.IsDocumentCreator(id))
            {
                var container = BL.DocumentBL.GetAllDocument(new BE.Document() { id = id });
                if (!container.HasErrors)
                {
                    Mapper.CreateMap<BE.Document, DocumentViewModel>();
                    DocumentViewModel instanceOfDestination = Mapper.Map<DocumentViewModel>(container.Value);
                    // fill values for dropdowns and co
                    instanceOfDestination.FillPredefinedValuesForUser(GetAdminOrVisibilityUnits());
                    return View(instanceOfDestination);
                }
                else
                {
                    SetError(container.ErrorMessage);
                    return RedirectToAction("Index");
                }
            }
            else
            {
                SetError("Vous n'avez pas le droit d'accéder à l'édition de ce document.");
                return RedirectToAction("Index");
            }
        }

编辑2:

[HttpPost]
        public ActionResult Edit(DocumentViewModel model)
        {
            if (User.IsInRole("Admin") || User.IsInRole("Moderator") || SessionManager.matricule.IsDocumentCreator(model.id))
            {
                Mapper.CreateMap<DocumentViewModel, BE.Document>();
                BE.Document instanceOfDestination = Mapper.Map<BE.Document>(model);
                Container<BE.Document> container = BL.DocumentBL.UpdateDocument(instanceOfDestination, new PersonnelAM() { id = SessionManager.matricule });
                if (!container.HasErrors)
                {
                    SetInfo("Modifications suavegardées");
                }
                else
                {
                    model.FillPredefinedValuesForUser(GetAdminOrVisibilityUnits());
                    SetError(container.ErrorMessage);
                    return View(instanceOfDestination);
                }
            }
            return RedirectToAction("Index");
        }

形式不绑定到正确的对象

您的视图模型绑定正确,但在 POST 方法中,这行代码

return View(instanceOfDestination);

返回Document的实例(由 BE.Document instanceOfDestination = Mapper.Map<BE.Document>(model); 定义),而不是导致异常的DocumentViewModel实例

字典中传递的模型类型为 BE。文档,但此字典需要类型为 DocumentViewModel 的模型

将其更改为

return View(model);

以便将正确的类型传递回视图。

请确保将

视图模型绑定到视图(cshtml文件),而不是模型,位于视图文件(例如edit.cshtml)之上:

@model your.namespace.DocumentViewModel

而不是

@model your.namespace.BE.Document

但是,如果视图模型与您的模型相同,为什么要使用视图模型?为什么不直接使用您的模型