Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Html.DropDownListFor to list data from one table, but save it to another
    text
    copied!<p>On the Create action, I can add information into editor fields and save them to Table1, however, for one of the fields I want a drop down list, populated from a column from Table2, which can then be selected and saved to Table1 in one of the fields, depending on selection. I have managed to display the information in a drop down table from Table2, but not pass it to Table1</p> <p>Model1:</p> <pre><code>public int id { get; set; } public string JobNo { get; set; } public string DelNo { get; set; } public Nullable&lt;int&gt; DeptToPack { get; set; } public string PackNo { get; set; } public Nullable&lt;decimal&gt; TargetHrs { get; set; } public Nullable&lt;System.DateTime&gt; PackingDate { get; set; } public virtual Table2 Table2{ get; set; } </code></pre> <p>Model 2:</p> <pre><code>public Table2() { this.Table1= new HashSet&lt;Table1&gt;(); } public int id { get; set; } public string Dept { get; set; } public virtual ICollection&lt;Table1&gt; Table1{ get; set; } </code></pre> <p>Combined Model:</p> <pre><code>public class Combined { public Table1 Table1 { get; set; } public Table2 Table2{ get; set; } } </code></pre> <p>The view currently displays text editors for all the fields which can be edited and saved to a database using SaveChanges(); on the HomeController, under a Create action</p> <p>Create Action</p> <pre><code>public ActionResult Create() { IEnumerable&lt;SelectListItem&gt; items = _db.Table2 .Select(c =&gt; new SelectListItem { Value = c.Dept, Text = c.Dept }); ViewBag.Dropdownlist = items; return View(); } [HttpPost] public ActionResult Create(Table1 table1) { if (ModelState.IsValid) { _db.Table1.Add(table1); _db.SaveChanges(); return RedirectToAction("Index"); } return View(table1); } </code></pre> <p>And the view displays the information from table1, and one field from table2. I seem to be having trouble with the @Html.DropDownList element</p> <p>I currently have (below) which displays the data, but cannot be processed to Table1</p> <pre><code> @Html.DropDownList("Table1.Field1", (IEnumerable&lt;SelectListItem&gt;) ViewBag.DropDownList) </code></pre> <p>Hopefully the image explains it better, filled input boxes will be saved to Table1 except for 1 field, which is a drop down list from column {InfoToPass}, from Table2, in which 1 of 4 options can be selected, then that value along with all the other inputs will be saved to Table1</p> <p><img src="https://i.stack.imgur.com/cO0At.png" alt="enter image description here"></p>
 

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