One-To-Many Relationship not editing Child table - asp.net

public class Class1
{
public Guid Class1ID { get; set; }
public string class1string { get; set; }
public virtual Class2 Class2 { get; set; }
}
public class Class2
{
public Guid Class2ID { get; set; }
public string class2string { get; set; }
}
// POST: Class1/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Class1ID,Class2,class1string")] Class1 class1)
{
if (id != class1.Class1ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(class1);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Class1Exists(class1.Class1ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Index");
}
return View(class1);
}
Instead of the Edit changing the data that is in the child table, it creates an new row in the table and changes the GUID in the parent table. The Parent table is edited correctly.
Any help will be greatly appreciated.

In your Class 1 also add the foreignkey id that should match the primary key property. EF will know its related
public class Class1
{
public Guid Class1ID { get; set; }
public string class1string { get; set; }
public Guid? Class2ID { get; set; }
[ForeignKey("Class2ID ")]//probably not needed as names match
public virtual Class2 Class2 { get; set; }
}
public class Class2
{
public Guid Class2ID { get; set; }
public string class2string { get; set; }
}
that way in your update class1 you just need to check you pass the correct Class2ID property and not worry about the navigation object property Class2.
For saving you need to spesify it was modified
public async Task<IActionResult> Edit(Class1 class1)
{
...
_context.Entry(class1).State = EntityState.Modified;
await _context.SaveChangesAsync();

I have also been struggling with this problem.
When I make an edit and save it I hit the function public async Task Edit(Guid id,[Bind("Class1ID,Class2,class1string")] Class1 class1) (as you would expect) -
All the values are correct except class1.Class2.Class2ID which is an empty guid {00000000-0000-0000-0000-000000000000}
As a result when SaveChangesAsync is called EF creates a new record for Class2, rather than updating the existing record as intended.
The is because the binding is failing, it can't find the value for Class2.Class2ID.
This is because the Get is failing to load this value as it is almost certainly not on the page.
Add the following line to your view markup (suggest next to input type="hidden" asp-for="Class1ID")
<input type="hidden" asp-for="Class2.Class2ID" />
This should enable the binding to work.
I hope this helps.

Related

Clean way for updating object in a collection of abstract objects

As I'm developping an asp net core + ef core 2.0 with localized objects in my model, I adapted the solution provided in the following link to localize my objects link.
I'm now trying to find a clean way to update my collection of translation when updated object are received in the controller.
For the moment I have a step model class defined this way :
public class Step
{
//Native properties
public Guid ID { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public string ScriptBlock { get; set; }
//Parent Step Navigation property
public Nullable<Guid> ParentStepID { get; set; }
public virtual Step ParentStep { get; set; }
//Collection of sub steps
public virtual ICollection<Step> SubSteps { get; set; }
//MUI Properties
public TranslationCollection<StepTranslation> Translations { get; set; }
public string Description { get; set; }
//{
// get { return Translations[CultureInfo.CurrentCulture].Description; }
// set { Translations[CultureInfo.CurrentCulture].Description = value; }
//}
public Step()
{
//ID = Guid.NewGuid();
Translations = new TranslationCollection<StepTranslation>();
}
}
public class StepTranslation : Translation<StepTranslation>
{
public Guid StepTranslationId { get; set; }
public string Description { get; set; }
public StepTranslation()
{
StepTranslationId = Guid.NewGuid();
}
}
Translation and translationCollection are the same as in the link
public class TranslationCollection<T> : Collection<T> where T : Translation<T>, new()
{
public T this[CultureInfo culture]
{
// indexer
}
public T this[string culture]
{
//indexer
}
public bool HasCulture(string culture)
{
return this.Any(x => x.CultureName == culture);
}
public bool HasCulture(CultureInfo culture)
{
return this.Any(x => x.CultureName == culture.Name);
}
}
public abstract class Translation<T> where T : Translation<T>, new()
{
public Guid Id { get; set; }
public string CultureName { get; set; }
protected Translation()
{
Id = Guid.NewGuid();
}
public bool HasProperty(string name)
{
return this.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Any(p => p.Name == name);
}
}
My issue in this sample is how to deal correctly with the PUT method and the Description property of my step controller. When it receive a Step object to update (which is done through a native c# client) only the string Description property of Step might have been created/updated/unchanged. So I have to update/create/do Nothing on the Description of the translation in the correct culture.
My first guess is to add in the TranslationCollection class a method in which I could pass the culture, the name of the property to update or not (Description in this case) and the value of the Description.
But as the TranslationCollection is a collection of abstract objects I don't even if this is a good idea and if it's possible.
If someone would have any advice on it (hoping I was clear enough) it would be great !
Finally answered my own question, and it was quite simple.
Just had to use the indexer like :
myobject.Translations[userLang].Name = value;

List of same model MVC 4

I have a model enrollment that have a list of components that have a list of subcomponents. This is what i have:
public class Enrollment
{
...
public virtual int ClassroomId { get; set; }
public virtual Classroom Classroom { get; set; }
public virtual List<Components> Components { get; set; }
}
public class Component
{
...
public virtual int EnrollmentId { get; set; }
public virtual Enrollment Enrollment{ get; set; }
public virtual List<Subcomponent> Subcomponents { get; set; }
}
public class Subcomponent
{
...
public virtual int ComponentId { get; set; }
public virtual Component Component{ get; set; }
public virtual List<Subcomponent> Subcomponent { get; set; } (???)
}
The user needs to be able to create subsubcomponents and so on, as much as he wants. For example, the enrollment Matematics have a component Test that have a subcomponent Group1 that can have a subsubcomponent Question 1 and a subsubsubcomponent Question 1.1 and so on.
I have this create methods on the Subcomponent controller:
public ActionResult CreateMoreSubcomponents(int id)
{
var x = db.Subcomponents.FirstOrDefault(e => e.SubcomponentId == id);
if (x == null)
{
return HttpNotFound();
}
var subcomponent = new Subcomponent()
{
ComponentId = x.ComponentId,
SubcomponentId = x.SubcomponentId
};
return View("CreateMoreSubcomponents", subcomponent);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMoreSubcomponents(Subcomponent subcomponent)
{
if (ModelState.IsValid)
{
db.Subcomponents.Add(subcomponent);
db.SaveChanges();
return RedirectToAction(...);
}
return View(subcomponent);
}
Note that this code may be all wrong, this was just a way i tried to do. The question is how can i do it? Am i close to the solution or not so close and if so how is it possible to do what i want? Thanks
EDIT:
I changed the code since it needs the componentId anyways. In this way it gives me an error on db.SaveChanges();
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Subcomponents_dbo.Components_ComponentId". The conflict occurred in database "SGPContext-20160719133511", table "dbo.Components", column 'ComponentId'. The statement has been terminated

MVC ASP.NET Entity Framework Not Saving a List of Assocciated Objects

This question is in reference to the project discussed here. After resolving the previous problem I have run into a new one. When The Student object is saved, the list of courses associated with it is not saved. I can see the collection of course objects when I mouse over the student object after setting a breakpoint:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddCourseVM (AddCourseViewModel vModel)
{
Student stu = db.Students.Find(vModel.Student.ID);
foreach (Course c in vModel.PossibleCourses)
{
if (c.Selected)
{
BaseCourse bc = db.BaseCourses.Find(c.BaseCourse.ID);
c.BaseCourse = bc;
c.Student = stu;
stu.CoursesTaken.Add(c);
}
}
if (stu != null)
{
db.Entry(stu).State = EntityState.Modified; //breakpoint here
db.SaveChanges();
}
return RedirectToAction("ListTakenCourses", stu);
}
public ActionResult ListTakenCourses (Student stu)
{
List<Course> taken = stu.CoursesTaken.ToList();
foreach (Course c in taken)
{
c.BaseCourse = db.BaseCourses.Find(c.BaseCourse.ID);
}
ViewBag.CoursesTaken = taken;
return View(stu);
}
But when I pass the object to the next method, the list of courses taken comes back null. The courses are being saved to the database, I can see them when I go into the SQL Server explorer, but for some reason they are not being attached to the student object. The code for the objects:
public class Student
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string WNumber { get; set; }
public int HoursCompleted { get; set; }
public double GPA { get; set; }
public Concentration StudentConcentration { get; set; }
public virtual ICollection<Course> CoursesTaken { get; set; }
public virtual ICollection<Course> CoursesRecommended { get; set; }
}
and:
public class Course
{
public int ID { get; set; }
public string Semester { get; set; }
public Grade? Grade { get; set; }
public bool Selected { get; set; }
public BaseCourse BaseCourse { get; set; }
public Student Student { get; set; }
}
Something that may be important, but that I don't really understand: when I look at the table for the Course object in the database, there are three columns, called Student_ID, Student_ID1, and Student_ID2. I assume they relate to the student associated with the object and the two ways it can be associated (recommended or taken), but the odd thing is that Student_ID is always null, while the other two sometimes have a value and sometimes do not. I have not even begun to implement the recommendation process, so there is no way that list is being filled.
I reworked the classes and now it seems to be working. I changed the Course object to:
public class Course
{
public int ID { get; set; }
public string Semester { get; set; }
public Grade? Grade { get; set; }
public bool Selected { get; set; }
public int BaseCourseID { get; set; }
public int StudentID { get; set; }
public BaseCourse BaseCourse { get; set; }
public Student Student { get; set; }
}
and the controller methods to:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddCourseVM (AddCourseViewModel vModel)
{
Student stu = db.Students.Find(vModel.Student.ID);
foreach (Course c in vModel.PossibleCourses)
{
if (c.Selected)
{
BaseCourse bc = db.BaseCourses.Find(c.BaseCourse.ID);
c.BaseCourse = bc;
c.Student = stu;
stu.CoursesTaken.Add(c);
db.Entry(c).State = EntityState.Added;
}
}
if (stu != null)
{
db.Entry(stu).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("ListTakenCourses", stu);
}
public ActionResult ListTakenCourses (Student stu)
{
List<Course> taken = db.Courses.Where(c => c.StudentID == stu.ID).ToList();
foreach (Course c in taken)
{
c.BaseCourse = db.BaseCourses.Find(c.BaseCourseID);
c.Student = stu;
stu.CoursesTaken.Add(c);
}
ViewBag.CoursesTaken = taken;
return View(stu);
}
And it is now displaying the courses I add on the next page, but it seems odd that I have to save the child objects separately from the parent and that I have to get the list from the database manually instead of being able to use the object structure. Is this intended behavior, or is there a better way of doing what I'm trying to do (add a list of child objects (courses) to a student object, save the relationship to the database, and then display the list of added objects)?
You are not "passing the object to the next method". You are serializing the object and passing it on the URL, then deserializing it on the other end with this method:
return RedirectToAction("ListTakenCourses", stu);
This is not the way to go about things. What you should be doing is passing a single id, such as the student id. Then, in ListTakenCourses you look up the student again in the database, which if you are doing your query correctly will fully populate the objects.
return RedirectToAction("ListTakenCourses", new { id = stu.StudentID });
public ActionResult ListTakenCourses (int id)
{
List<Course> taken = db.Courses.Where(c => c.StudentID == id).ToList();
//...
}

How to Preserve/Protect Certain Fields in Edit in ASP.NET MVC

In an Edit action in ASP.NET MVC, certain fields can be hidden from user with HiddenFieldFor. However this doesn't protect the fields (such as ID, data creation date) from being edited.
For example, a model Student has fields Id, Name and Birthday. I like to allow users to update the Name, but not Id nor Birthday.
For an Edit action like this
public ActionResult Edit(Student student)
{
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
How can I prevent Id and Birthday from being edited? Thanks!
You should use a view model which contains only the properties that you want to be edited:
public class EditStudentViewModel
{
public string Name { get; set; }
}
and then:
public ActionResult Edit(StudentViewModel student)
{
...
}
Another technique which I don't recommend is to exclude certain properties from binding:
public ActionResult Edit([Bind(Exclude = "Id,Birthday")]Student student)
{
...
}
or include:
public ActionResult Edit([Bind(Include = "Name")]Student student)
{
...
}
I assume you have to have the properties in your Model so that in View you can use them to render useful information e.g. an ActionLink with ID or some readonly Text.
In this case you can define your model with an explicit binding:
[Bind(Include = "Name")]
public class Student
{
int Id { get; set; }
int Name { get; set; }
DateTime Birthday { get; set; }
}
This way when updating your model, if the user submits an extra Id it will not be bound.
Another idea I like is having your model know its bindings per scenario and have them compiler validated:
public class ModelExpression<T>
{
public string GetExpressionText<TResult>(Expression<Func<T, TResult>> expression)
{
return ExpressionHelper.GetExpressionText(expression);
}
}
public class Student
{
public static string[] EditBinding = GetEditBinding().ToArray();
int Id { get; set; }
int Name { get; set; }
DateTime Birthday { get; set; }
static IEnumerable<string> GetEditBinding()
{
ModelExpression<Student> modelExpression = new ModelExpression<Student>();
yield return modelExpression.GetExpressionText(s => s.Name);
}
}
This way in your Action when calling TryUpdateModel you can pass this information.

UpdateModel has no effect in my action controller after posting new values

Below is a portion of my controller:
[Authorize]
public ActionResult Edit(string IdAffaire)
{
Affaire affaire = this.repository.Retrieve(IdAffaire);
if (affaire == null)
{
return Redirect("~/");
}
var model = new AffaireEditViewModel
{
Affaire = affaire,
Status = repository.RetrieveStatus().Select(o => new SelectListItem { Text = o.Name, Value = o.IdStatus.ToString() }).ToList(),
};
return View(model);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(string idAffaire, AffaireEditViewModel model)
{
Affaire affaire = repository.Retrieve(idAffaire);
if (!ModelState.IsValid)
{
return this.Edit(model.Affaire.IdAffaire);
}
try
{
UpdateModel(affaire);
repository.Save();
return RedirectToAction("Detail", "Affaire", new { idAffaire = idAffaire });
}
catch
{
return View(affaire);
}
}
Below is my ViewModel for edit:
public class AffaireEditViewModel
{
public Affaire Affaire { get; set; }
public IEnumerable<SelectListItem> Status { get; set; }
}
Below is my Affaire model:
public class Affaire
{
[Key]
public string IdAffaire { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Username { get; set; }
public Int16? IdStatus { get; set; }
public Int16? IdLabel { get; set; }
// ....
}
My problem is that when posting new values in my edit view page, the action named Edit is well triggered (posting) with right values, but the statement UpdateModel(affaire) has no effect! Any help is greatly appreciated.
EDITED
I found the problem.
I need to change from this:
UpdateModel(affaire);
To this:
UpdateModel(affaire,"Affaire");
I guess it is because my view model is composed of several things and I need to tell explicitly to my UpdateModel function which element to use. Can somebody confirm?
To verify - is your repository retaining a reference to that instance of the model? I see you call save - but I don't see the implementation of the save since you aren't passing in a model to it.

Resources