C# 表单未绑定到正确的对象
我有类似的对象(我使用模型和视图模型,其中视图模型与模型对象具有相同的属性) 当我编辑一个项目时,我放置viewmodel的编辑器,处理更新/编辑的控制器接收viewmodel:C# 表单未绑定到正确的对象,c#,asp.net,asp.net-mvc,modelbinders,C#,Asp.net,Asp.net Mvc,Modelbinders,我有类似的对象(我使用模型和视图模型,其中视图模型与模型对象具有相同的属性) 当我编辑一个项目时,我放置viewmodel的编辑器,处理更新/编辑的控制器接收viewmodel: [HttpPost] public ActionResult Edit(DocumentViewModel model) { Mapper.CreateMap < DocumentViewModel, BE.Document > (); BE.Document instanceOfDestination
[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");
}
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; }
以下是返回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");
}
}
[HttpGet]
公共操作结果编辑(int id){
var container=BL.DocumentBL.GetAllDocument(新的BE.Document(){
id=id
});
如果(!container.HasErrors){
Mapper.CreateMap();
DocumentViewModel instanceOfDestination=Mapper.Map(container.Value);
//下拉列表和co的填充值
instanceOfDestination.FillPredefinedValuesForUser(GetAdminOrVisibilityUnits());
返回视图(instanceOfDestination);
}否则{
SetError(container.ErrorMessage);
返回操作(“索引”);
}
}
文档有模型和视图模型:
DocumentViewModel:
[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");
}
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;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串引用{get;set;}
[必需]
[范围(0,100)]
公共int循环span{get;set;}
[必需]
公共bool isActive{get;set;}
[DocumentTypeValidator(“需要DocType”)]//自定义验证器
公共DocumentType DocumentType{get;set;}
公共人员培训师{get;set;}
公共列表版本{get;set;}
公共列表安装{get;set;}
公共列表{get;set;}
公共列表主题{get;set;}
公共单位单位{get;set;}
//不用于编辑或创建
公共人员由{get;set;}创建
public DateTime createdOn{get;set;}
公共人员由{get;set;}编辑
public DateTime editedOn{get;set;}
//填充下拉列表
公共IEnumerable文档类型选择{get;set;}
公共IEnumerable personnelSelect{get;set;}
公共IEnumerable安装选择{get;set;}
public IEnumerable professions选择{get;set;}
公共IEnumerable topicTypeSelect{get;set;}
公共IEnumerable单元选择{get;set;}
//对于多重选择-使用FillListsFromIds从ID填充列表
public int[]selectedProfessions{get;set;}
public int[]selectedInstallations{get;set;}
public int[]selectedTopics{get;set;}
//用于文件上载
[Minlengthatt贡(1)]
公共HttpPostedFileBase[]文件{get;set;}
//用于文件获取
公共列表文件列表{get;set;}
BE.文件
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; }
public int id{get;set;}
公共字符串名称{get;set;}
公共字符串引用{get;set;}
公共int循环span{get;set;}
公共bool isActive{get;set;}
公共DocumentType DocumentType{get;set;}
公共人员培训师{get;set;}
公共列表文件列表{get;set;}
公共列表安装{get;set;}
公共列表版本{get;set;}
公共列表{get;set;}
公共列表主题{get;set;}
公共单位单位{get;set;}
公共人员由{get;set;}创建
public DateTime createdOn{get;set;}
公共人员由{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");
}
}
[HttpGet]
公共操作结果编辑(int id)
{
if(User.IsInRole(“Admin”)| User.IsInRole(“主持人”)| | SessionManager.matricule.IsDocumentCreator(id))
{
var container=BL.DocumentBL.GetAllDocument(新的BE.Document(){id=id});
如果(!container.HasErrors)
{
CreateMap();
DocumentViewModel instanceOfDestination=Mapper.Map(container.Value);
//下拉列表和co的填充值
instanceOfDestination.FillPredefinedValuesForUser(GetAdminOrVisibilityUnits());
返回视图(instanceOfDestination);
}
其他的
{
SetError(container.ErrorMessage);
返回操作(“索引”);
}
}
其他的
{
SetError(“您可以通过修改文件的权利”);
返回操作(“索引”);
}
}
编辑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");
}
[HttpPost]
公共操作结果编辑(DocumentViewModel模型)
{
if(User.IsInRole(“Admin”)| User.IsInRole(“主持人”)| | SessionManager.matricule.IsDocumentCreator(model.id))
{
CreateMap();
BE.Document instanceOfDestination=Mapper.Map(model);
Container Container=BL.DocumentBL.UpdateDocument(instanceOfDestination,newpersonnelam(){id=SessionManager.matricule});
如果(!container.HasErrors)
{
SetInfo(“修正案”);
}
其他的
{
model.FillPredefinedValuesForUser(GetAdminOrVisibilityUnits());
SetError(container.ErrorMessage);
返回视图(instanceOfDestination);
}
}
返回操作(“索引”);
}
请确保将视图模型绑定到视图文件(例如edit.cshtml)之上的视图(cshtml文件),而不是模型:
而不是
@model your.namespace.BE.Document
但是,如果viewmodel与您的模型相同,为什么要使用viewmodel?为什么不直接使用您的模型呢?请确保在视图文件(例如edit.cshtml)的顶部将视图模型绑定到视图(您的cshtml文件),而不是您的模型: 而不是
@model your.namespace.BE.Document
但是,如果viewmodel与您的模型相同,为什么要使用viewmodel?为什么不直接使用您的模型呢您的视图模型绑定正确,但是在POST方法中,这一行代码
return View(instanceOfDestination);
正在返回Docu的实例