Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to map a model with reference to a list of another model in EntityFramework Code First
    primarykey
    data
    text
    <p>I have a project in which I am using EntityFramework Code First to manage the database. The models I'm using for this are as follows:</p> <pre><code>[Table("Data")] public class Data { [Key, Column("Id")] public int Id { get; set; } [Column("Code")] public string Code { get; set; } [Column("Name")] public string Name { get; set; } [Column("Description")] public string Description { get; set; } public virtual List&lt;DataAttribute&gt; Attributes { get; set; } } </code></pre> <p>and</p> <pre><code>[Table("DataAttribute")] public class DataAttribute { [Key, Column("Id")] public int Id { get; set; } [Column("IdData"), ForeignKey("Data")] public int IdData { get; set; } [Column("Name")] public string Name { get; set; } [Column("Description")] public string Description { get; set; } public virtual Data Data { get; set; } } </code></pre> <p>The issue I'm having with this is when I try to edit a Data object (along with its related DataAttribute values). I haven't gotten it to map correctly when submitting a form with both the Data elements and the DataAttribute elements. I give an example of .cshtml file.</p> <pre><code>@model MVCTestApp.Models.Data @{ ViewBag.Title = "Edit"; } @section Scripts { &lt;script type="text/javascript"&gt; $(function () { $('#new-row').click(function () { $('table tbody').append( '&lt;tr&gt;' + '&lt;td&gt;&lt;input class="name" type="text" /&gt;&lt;/td&gt;' + '&lt;td&gt;&lt;input class="description" type="text" /&gt;&lt;/td&gt;' + '&lt;td&gt;&lt;a class="delete" href="#"&gt;Delete&lt;/a&gt;&lt;/td&gt;' + '&lt;/tr&gt;' ); $('.delete').click(function () { $(this).parent().parent().remove(); }); }); }); &lt;/script&gt; } @using (@Html.BeginForm("Edit", "Data", FormMethod.Post)) { @Html.HiddenFor(a =&gt; a.Id) Name: &lt;br /&gt; @Html.TextBoxFor(a =&gt; a.Name) &lt;br /&gt; Description: &lt;br /&gt; @Html.TextBoxFor(a =&gt; a.Description) &lt;br /&gt; Code: &lt;br /&gt; @Html.TextBoxFor(a =&gt; a.Code) &lt;br /&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt; Name &lt;/th&gt; &lt;th&gt; Description &lt;/th&gt; &lt;th&gt; &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;a id="new-row" href="#"&gt;New Row&lt;/a&gt; &lt;/td&gt; &lt;td colspan="2"&gt; &lt;/td&gt; &lt;/tr&gt; @if (Model != null) { foreach (var item in Model.Attributes) { &lt;tr&gt; @Html.HiddenFor(b =&gt; item.Id) @Html.HiddenFor(b =&gt; item.IdData) &lt;td class="name"&gt;@Html.TextBoxFor(b =&gt; item.Name) &lt;/td&gt; &lt;td class="description"&gt;@Html.TextBoxFor(b =&gt; item.Description) &lt;/td&gt; &lt;td&gt; &lt;a class="delete" href="#"&gt;Delete&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; } } &lt;/tbody&gt; &lt;/table&gt; &lt;input type="submit" value="submit" /&gt; } </code></pre> <p>The question is this. </p> <p><strong>How can I make EntityFramework recognize the list of DataAttributes on the Controller when posting?</strong></p> <p>Here's the code I'm using right now.</p> <pre><code> [HttpPost] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Data model) { if (model != null) { db.Data.Add(model); //model has the model.Attributes list set as null db.SaveChanges(); return View("Index", db.Data.OrderBy(a =&gt; a.Id).ToList()); } return View(); } </code></pre> <p>Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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