Answered by:
MVC Unable to save changes

Question
-
User1124429227 posted
Hi
I am trying to post a value of "2" to another model that is not mapped with a foreign key. However on clicking create the validation error comes back as unable to save changes. I believe this is a concurrency issue, due to the the following lines of code:
newClinicalAsset.ModelAssetAssignmentID = clinicalAssets.ModelAssetAssignmentID; newClinicalAsset.ModelName = clinicalAssets.ModelName;
I believe it is trying to post twice into the database when looking at the result both the ModelAssetAssignmentID & ModelName dont seam to be together.
Model ClinicalAssets:
[NotMapped] public virtual Model ModelAssetAssignmentID { get; set; }
Models:
using System.ComponentModel.DataAnnotations; namespace Assets.Models { public class Model { [Key] public int ModelID { get; set; } public int ModelAssetAssignmentID { get; set; } public string ModelName { get; set; } } }
Create.cshtml:
@Html.HiddenFor(model => model.ModelAssetAssignmentID, new { @Value = "2" });
Controller:
if (ModelState.IsValid) { var newClinicalAsset = new ClinicalAsset(); if (clinicalAssets.ModelName != null) { newClinicalAsset.ModelAssetAssignmentID = clinicalAssets.ModelAssetAssignmentID;
newClinicalAsset.ModelName = clinicalAssets.ModelName; } db.ClinicalAssets.Add(newClinicalAsset); db.SaveChanges(); return RedirectToAction("Details", "ClinicalAssets", new { ClinicalAssetID = clinicalAssets.ClinicalAssetID }); } }
catch (DataException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}Wednesday, August 21, 2019 10:22 AM
Answers
-
User1124429227 posted
The answer was to remove the [Not Mapped] & to create a second FK. Although i find this odd!
the following code saves the data correctly in the database table.
if (ModelState.IsValid) { var newClinicalAsset = new ClinicalAsset(); var NewModel = new Model(); if (clinicalAssetVM.AssetTypeID == 0) { } else { newClinicalAsset.AssetTypeID = clinicalAssetVM.AssetTypeID; } if (clinicalAssetVM.ModelName != null) { NewModel.ModelAssetAssignmentID = clinicalAssetVM.ModelAssetAssignmentID.ModelAssetAssignmentID; NewModel.ModelName = clinicalAssetVM.ModelName.ModelName; db.Models.Add(NewModel); db.SaveChanges(); } newClinicalAsset = new ClinicalAsset { ModelID = NewModel.ModelID, AssetTypeID = newClinicalAsset.AssetTypeID}; db.ClinicalAssets.Add(newClinicalAsset); db.SaveChanges(); return RedirectToAction("Details", "ClinicalAssets", new { ClinicalAssetID = clinicalAssetVM.ClinicalAssetID }); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 27, 2019 3:17 PM
All replies
-
User753101303 posted
Hi,
the validation error comes back as unable to save changesDon't focus on the code too early. Take first a couple of minutes to better understand (and explain) the problem you have. For both you and others, it is much easier to solve a problem knowing first what it is exactly rather than trying to guess from the code:
- if you mean IsValid returns false, inspect ModelState.Errors to see what exactly is wrong
- if you have an exception use ex.ToString() to get the full exception details
- if ModelName is null you won't assign values but you still try to save a newly created object which likely won't work (more likely ModelName should be a "required" field ?)
- if none of this, really start by telling us what happensWednesday, August 21, 2019 6:28 PM -
User1124429227 posted
HiDon't focus on the code too early. Take first a couple of minutes to better understand (and explain) the problem you have. For both you and others, it is much easier to solve a problem knowing first what it is exactly rather than trying to guess from the code:- if you mean IsValid returns false, inspect ModelState.Errors to see what exactly is wrong- if you have an exception use ex.ToString() to get the full exception details- if ModelName is null you won't assign values but you still try to save a newly created object which likely won't work (more likely ModelName should be a "required" field ?)- if none of this, really start by telling us what happensThe Model IsValid Returns True, after the first IF it is thrown into the the catchcatch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); }
ELMAH shows no entry. If i turn off the catch, The valdiation error is "The ModelAssetAssignmentID field is required"Wednesday, August 21, 2019 9:47 PM -
User-474980206 posted
so
if (ModelState.IsValid) {
is false so the error is thrown outside the shown if block. simple debugging should of shown this. as all value types (int, decimal, DateTime, etc) are required (because they can not be null), they will all be validation errors if empty.
Wednesday, August 21, 2019 10:23 PM -
User1124429227 posted
if i modify @Html.HiddenFor(model => model.ModelAssetAssignmentID.ModelAssetAssignmentID, new { id = "2" }); to the following @Html.HiddenFor(model => model.ModelAssetAssignmentID, new { id = "2" });
then
newClinicalAsset.ModelAssetAssignmentID = clinicalAssets.ModelAssetAssignmentID; is null.
Wednesday, August 21, 2019 11:57 PM -
User1520731567 posted
Hi microchef,
Your entity should be one to one or one to many,you may be missing navigation properties.
I suggest you could refer to this tutorial,and try to remove [NotMapped].
if (clinicalAssets.ModelName != null) { newClinicalAsset.ModelAssetAssignmentID = clinicalAssets.ModelAssetAssignmentID; newClinicalAsset.ModelName = clinicalAssets.ModelName; }
According to your model,you need to modify,like:
newClinicalAsset.ModelAssetAssignmentID = clinicalAssets.ModelAssetAssignmentID.ModelAssetAssignmentID;
If you still have any questions,please post more code,your complete entities,view and action.
Best Reagrds.
Yuki Tao
Thursday, August 22, 2019 8:23 AM -
User1124429227 posted
Hi Yuki
I cant use your suggestion as then i can't implicitly convert type int to Assets.Models.Model.
newClinicalAsset.ModelAssetAssignmentID = clinicalAssets.ModelAssetAssignmentID;
It should be a many to one relationship as i can have many assets but only one defined Model Types per asset. I am using the [Not Mapped] because a foreign key is being added to the clinical assets table, when a relationship PK & FK already exists for ModelID between the ClinicalAssets Table and the Models Table, why would i need another one in the same tables that are already related, or am i missing some knowledge?
or i guess what i'm trying to say, is it normally to have two FK relationships when it brings no insight to the data other than helping with filtering drop downs for different departmental users.
example:
private void PopulateModelDropDownList(object selectedModel = null) { var ModelsQuery = from d in db.Models.Where(x => x.ModelAssetAssignmentID == 2) orderby d.ModelName select d; ViewBag.ModelDropDown = new SelectList(ModelsQuery, "ModelID", "ModelName", selectedModel); }
Thursday, August 22, 2019 12:26 PM -
User1124429227 posted
Hi Yuki
I have since made some changes and i can save the ModelAssetAssignmentID but now the Model name and model assignment appear in different rows of the sql table rather than together in the same row.
if (ModelState.IsValid) { using (var db = new ClinicalContext()) { var newClinicalAsset = new ClinicalAsset(); if (clinicalAsset.AssetTypeID == 0) { } else { newClinicalAsset.AssetTypeID = clinicalAsset.AssetTypeID; } if (clinicalAssetVM.ModelName != null) { newClinicalAsset.ModelAssetAssignmentID = clinicalAsset.ModelAssetAssignmentID; newClinicalAsset.ModelName = clinicalAsset.ModelName; db.ClinicalAssets.Add(newClinicalAsset); db.SaveChanges(); } db.ClinicalAssets.Add(newClinicalAsset); } db.SaveChanges(); return RedirectToAction("Details", "ClinicalAssets", new { ClinicalAssetID = clinicalAssetVM.ClinicalAssetID }); }
Tuesday, August 27, 2019 10:17 AM -
User1124429227 posted
The answer was to remove the [Not Mapped] & to create a second FK. Although i find this odd!
the following code saves the data correctly in the database table.
if (ModelState.IsValid) { var newClinicalAsset = new ClinicalAsset(); var NewModel = new Model(); if (clinicalAssetVM.AssetTypeID == 0) { } else { newClinicalAsset.AssetTypeID = clinicalAssetVM.AssetTypeID; } if (clinicalAssetVM.ModelName != null) { NewModel.ModelAssetAssignmentID = clinicalAssetVM.ModelAssetAssignmentID.ModelAssetAssignmentID; NewModel.ModelName = clinicalAssetVM.ModelName.ModelName; db.Models.Add(NewModel); db.SaveChanges(); } newClinicalAsset = new ClinicalAsset { ModelID = NewModel.ModelID, AssetTypeID = newClinicalAsset.AssetTypeID}; db.ClinicalAssets.Add(newClinicalAsset); db.SaveChanges(); return RedirectToAction("Details", "ClinicalAssets", new { ClinicalAssetID = clinicalAssetVM.ClinicalAssetID }); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 27, 2019 3:17 PM