Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC newbie: getting/passing data in dropdown form for multiple objects in an edit View
    primarykey
    data
    text
    <p>Using the Authors/Books catalog example, let's say I want to edit the info for the books of a specific author.</p> <p>When someone navigates to domain.com/Books/Edit/2, I want to display an edit view for all the books where Author_ID = 2. Among the various book info is the book category (fiction, non-fiction, textbook, whatever) These categories are in their own table and are referenced by a Category_ID.</p> <p>What's the best way to set up the edit form?</p> <p>Currently in my controller I have something like this:</p> <pre><code>public ActionResult Edit(int id) { IQueryable&lt;Book&gt; books = bookRepository.FindBooksForAuthor(id); return View(books); } </code></pre> <p>And in my partial view:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;IQueryable&lt;Authors.Models.Book&gt;&gt;" %&gt; &lt;%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;%var i = 0; foreach (var book in Model) {%&gt; &lt;p&gt; &lt;label for="Category_ID"&gt;Category_ID:&lt;/label&gt; &lt;%= Html.TextBox("Category_ID", book.Category_ID)%&gt; &lt;%= Html.ValidationMessage("Category_ID", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Description"&gt;Description:&lt;/label&gt; &lt;%= Html.TextBox("Description", book.Description)%&gt; &lt;%= Html.ValidationMessage("Description", "*")%&gt; &lt;/p&gt; &lt;%i++; } %&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; </code></pre> <p>Is my <code>Inherits</code> set properly at the top of the view since I'm passing an <code>IQueryable</code> object?</p> <p>More importantly, how do I get the Category_ID field to be a DropDown with the correct category selected? Can I just send the data for the dropdown to the view and figure out the selected item at the view level?</p> <pre><code>ViewData["categories"] = new SelectList(_db.BookCategories.ToList().OrderBy(b =&gt; b.Category_Title), "Category_ID", "Category_Title"); </code></pre>
    singulars
    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