I’m having trouble figuring out how to get my navigation property to update with the Entity Framework. I used a database first approach and set up all appropriate FK relationships. Here’s what the two tables I’m working with look like:
Rate Profile
- RateProfileID
- ProfileName
Rate
- RateID
- RateProfileID (FK)
- Several Other Properties I Want to Update
One RateProfile can/will have many Rates. I built my edit page for RateProfile to display editors for the RateProfile Entity and all of it’s associated Rate entities and stuck all of that in a form with a submit button. I can display everything just fine, but my changes will only persist for the model class (RateProfile) and not for its navigation property (Rates).
Below are my views/HttpPost Edit/ Models
In my HttpPost Edit method, you can see my feeble attempt to loop through and Update each record in the navigation property Rates of the model.
@model PDR.Models.RateProfile
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>RateProfile</legend>
@Html.HiddenFor(model => model.RateProfileID)
@Html.HiddenFor(model => model.LoginID)
<div class="editor-label">
@Html.LabelFor(model => model.ProfileName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProfileName)
@Html.ValidationMessageFor(model => model.ProfileName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.isDefault)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.isDefault)
@Html.ValidationMessageFor(model => model.isDefault)
</div>
<div>
<fieldset>
<legend>Dime</legend>
<table>
<tr>
<th>
Min
</th>
<th>
Max
</th>
<th>
Price
</th>
<th></th>
</tr>
@foreach (var rate in Model.Rates)
{
<tr>
<td>
@Html.EditorFor(modelItem => rate.minCount)
@Html.ValidationMessageFor(model => rate.minCount)
</td>
<td>
@Html.EditorFor(modelItem => rate.maxCount)
@Html.ValidationMessageFor(model => rate.maxCount)
</td>
<td>
@Html.EditorFor(modelItem => rate.Amount)
@Html.ValidationMessageFor(model => rate.Amount)
</td>
</tr>
}
</table>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
[HttpPost]
public ActionResult Edit(RateProfile rateprofile)
{
if (ModelState.IsValid)
{
db.Entry(rateprofile).State = EntityState.Modified;
foreach (Rate rate in rateprofile.Rates)
{
db.Entry(rate).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(rateprofile);
}
public partial class Rate
{
public int RateID { get; set; }
public int RateProfileID { get; set; }
public string Size { get; set; }
public decimal Amount { get; set; }
public int minCount { get; set; }
public int maxCount { get; set; }
public int PanelID { get; set; }
public virtual Panel Panel { get; set; }
public virtual RateProfile RateProfile { get; set; }
}
public partial class RateProfile
{
public RateProfile()
{
this.Rates = new HashSet<Rate>();
}
public int RateProfileID { get; set; }
public string ProfileName { get; set; }
public int LoginID { get; set; }
public bool isDefault { get; set; }
public virtual Login Login { get; set; }
public virtual ICollection<Rate> Rates { get; set; }
}
You change the
foreachintoforstatement and try whether the model binding is working fine or not.