Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen using ASP.NET MVC 3 with EF 4.1 Code First, I can only edit the main table, what am I doing wrong?
    text
    copied!<p>A brief description of what I am doing. I am creating a rather crude IS Asset tracking database using ASP MVC 3 and EF Code First approach. I can create a new asset. I can view the details on an asset. I can even display the edit view and edit the AssetTag. However the record will not update any of the other fields. If I edit the LocationName for instance. It will act like it is posting and return me to the Index view, but the record never actually posts the change. </p> <p>I have created the Model below</p> <pre><code>public class AssetModel { public int id { get; set; } public string AssetTag { get; set; } public virtual Location Location { get; set; } public virtual Hardware Hardware { get; set; } public virtual Software Software { get; set; } public virtual User User { get; set; } } public class Location { public int LocationId { get; set; } public string LocationName { get; set; } } public class Hardware { public int HardwareId { get; set; } public string Manufacturer { get; set; } public string Make { get; set; } public string Model { get; set; } } public class Software { public int SoftwareId { get; set; } public string PublisherName { get; set; } public string SoftwarePackageName { get; set; } public string SoftwarePackageVersion { get; set; } public string SerialNumber { get; set; } public bool IsVolumeLicense { get; set; } // as in "Yes this is a Vol. Lic. Agreement" public LicenseAgreement LicenseAgreement { get; set; } } public class LicenseAgreement { public int LicId { get; set; } public string VolumeLicenseAgreementCompany { get; set; } public string AgreementIdentifier { get; set; } public DateTime VolumeLicenseStartDate { get; set; } public DateTime VolumeLicenseExpirationDate { get; set; } public Int16 NumberOfLicenses { get; set; } } public class User { // may remove this at some time and pull from Active Directory. // for now we take the easy route. public int UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } </code></pre> <p>I have this DbDataSet that uses the AssetModel above:</p> <pre><code>public class AssetContext : DbContext { public DbSet&lt;AssetModel&gt; Assets { get; set; } } </code></pre> <p>In my AssetController I have this for Edit:</p> <pre><code>public ActionResult Edit(int id) { AssetModel assetmodel = db.Assets.Find(id); return View(assetmodel); } // // POST: /Asset/Edit/5 [HttpPost] public ActionResult Edit(AssetModel assetmodel) { if (ModelState.IsValid) { db.Entry(assetmodel).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(assetmodel); } </code></pre> <p>And here is the Edit.cshtml</p> <pre><code>@model ISHelpDesk.Models.AssetModel @{ ViewBag.Title = "Edit"; } &lt;h2&gt;Edit&lt;/h2&gt; </code></pre> <pre><code>@using (Html.BeginForm("Edit", "Asset")) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;AssetModel&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.AssetTag) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.AssetTag) @Html.ValidationMessageFor(model =&gt; model.AssetTag) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Location.LocationName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Location.LocationName) @Html.ValidationMessageFor(model =&gt; model.Location.LocationName) &lt;/div&gt; &lt;/fieldset&gt; &lt;p&gt;&lt;input type="submit" value="Save"&lt;/p&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload