表单不绑定到正确的对象

时间:2016-02-01 08:23:40

标签: c# asp.net asp.net-mvc modelbinders

我有类似的对象(我使用的是Model和ViewModels,其中ViewModels与Model对象具有相同的属性)。

当我编辑一个项目时,我为viewmodel编写了编辑器,并且处理更新/编辑的控制器,他收到了ViewModel:

[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.Document而不是DocumentViewModel。

以下是浏览器发送的值:

__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以进行编辑页面的Controller:

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

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

DocumentViewModel:

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

BE.Document

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

2 个答案:

答案 0 :(得分:1)

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

return View(instanceOfDestination);

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

  

字典中传递的模型是BE.Document类型,但是这个字典需要DocumentViewModel类型的模型

将其更改为

return View(model);

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

答案 1 :(得分:-1)

请确保在View文件(例如edit.cshtml)之上将viewmodel绑定到您的视图(您的cshtml文件),而不是您的模型:

@model your.namespace.DocumentViewModel

而不是

@model your.namespace.BE.Document

但是,如果viewmodel与您的模型完全相同,为什么要使用viewmodel?为什么不只是使用你的模型