简体   繁体   English

ASP.NET MVC5-可空模型属性在编辑模型时始终丢失其值(空)

[英]ASP.NET MVC5 - Nullable model property always losing its value (null) when editing model

When I Edit my model, all properties are saved correctly except for a nullable property which always loses its value in the process. 当我编辑模型时,所有属性均正确保存,但可为空的属性在过程中始终会丢失其值。

I have tried to hide the property in my view as such: 我试图这样隐藏属性:

@Html.HiddenFor(model => model.UserId)

In my controller's Edit method I tried to: 在控制器的Edit方法中,我尝试执行以下操作:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ItemViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var item= db.Items.Find(viewModel.ItemId);
                Mapper.Map<ItemViewModel, Item>(viewModel, item);
                //db.Entry(item).State = EntityState.Modified;
                if (TryUpdateModel(item))
                {
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            ViewBag.UserId = new SelectList(db.Users, "UserId", "Username", viewModel.UserId);
            return View(viewModel);
        }

I also tried without the TryUpdateModel and with the UpdateModel(model) method. 我还尝试了没有TryUpdateModel和带有UpdateModel(model)方法的情况。 I even tried reassign it the property value from the ViewModel to the Model manually just before saving the changes. 在保存更改之前,我什至尝试将其从ViewModel的属性值重新分配给Model。

When I put breakpoints throughout the method I can see that the property still holds its value but as soon as I hit Save it becomes null. 当我在整个方法中放置断点时,我可以看到该属性仍然保留其值,但是当我点击“保存”后,该属性就会为空。

Note that if I uncomment the EntityState.Modified line I am no longer losing the property value but I am handling this stuff in my DbContext (I have different type of edit events for different property and having this line of code uncommented in my controller seems to triger all of my edit events even if nothing changed). 请注意,如果我取消对EntityState.Modified行的注释,我将不再失去属性值,而是在DbContext中处理这些内容(我对不同的属性具有不同类型的编辑事件,并且在控制器中取消注释了这行代码,触发我所有的编辑事件,即使没有任何更改也是如此。

Please note that this property is also a Foreign key (not sure if it actually matters here) 请注意,此属性也是外键(不确定此处是否确实重要)

TryUpdateModel doesn't actually do anything to the item passed into it. TryUpdateModel实际上对传递给它的项目不做任何事情。 It merely runs model validation on it. 它仅对其运行模型验证。 Whatever values were there, remain there. 无论那里有什么价值,都留在那里。

I think I know what the issue is, and it's related to AutoMapper. 我知道问题出在哪里,这和AutoMapper有关。 Your posted model most likely doesn't include the actual related user object, just the id you posted for that. 您发布的模型很可能不包含实际的相关用户对象,而仅包含您为此发布的ID。 When you map your model onto your entity, AutoMapper is replacing the value for the related user object with null , since the model doesn't have it. 当您将模型映射到实体上时,AutoMapper会将相关用户对象的值替换为null ,因为模型没有此值。 Try adding the following to your CreateMap : 尝试将以下内容添加到您的CreateMap

.ForMember(dest => dest.User, opts => opts.Ignore());

What I am saying is all of your code is correct except for this line: 我的意思是,除了以下这一行以外,您所有的代码都是正确的:

 Mapper.Map<ItemViewModel, Item>(viewModel, item);

That line should be 那条线应该是

 item = Mapper.Map<ItemViewModel, Item>(viewModel, item);

Then at some point you need to pass that to the repository: 然后,您需要将其传递给存储库:

 db.Update(item);
 db.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM