Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get selected drop down value to bind to view model property
    primarykey
    data
    text
    <p>I'm having trouble binding the selected value of a drop down list to the correct property in my view model. I can't see what I am doing wrong here. I've put the code that should help show what I'm doing below. I've omitted some things such as the population of the 'AllFolders' property of the view model, as it's just a simple List with an object called <code>ImageGalleryFolder</code>.</p> <p>Every time the form posts back, the <code>ParentFolderId</code> property is null without fail. This is driving me crazy and I've wasted a lot of time trying to work it out.</p> <p>Can anyone see something I'm doing wrong?</p> <p>This is the view model</p> <pre><code>public class ImageGalleryFolderViewModel { [Required] public string Title { get; set; } public int Id { get; set; } public string CoverImageFileName { get; set; } public HttpPostedFileBase UploadedFile { get; set; } public string ParentFolderId { get; set; } public IList&lt;ImageGalleryFolder&gt; AllFolders { get; set; } } </code></pre> <p>Here is the view code</p> <pre><code>@using Payntbrush.Presentation.Demo.MVC3.Areas.Admin @model Payntbrush.Presentation.Demo.MVC3.Areas.Admin.Models.ImageGalleryFolderViewModel @{ ViewBag.Title = "Create A New Gallery Folder"; } &lt;h2&gt;@ViewBag.Title&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm((string)ViewBag.Action + "Folder", "Portfolio", FormMethod.Post, new { Id = "CreateFolder", enctype = "multipart/form-data" })) { @Html.ValidationSummary(true) if(((string)ViewBag.Action).ToLower() == FormConstants.Edit.ToLower()) { @Html.HiddenFor(m =&gt; m.Id) @Html.HiddenFor(m =&gt; m.CoverImageFileName) @Html.HiddenFor(m =&gt; m.ParentFolderId) } &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Title) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Title) @Html.ValidationMessageFor(model =&gt; model.Title) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.UploadedFile) &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input type="file" name="UploadedFile"/&gt; @Html.ValidationMessageFor(model =&gt; model.UploadedFile) &lt;/div&gt; { // Count &gt; 1 is important here. If there is only 1 folder, then we still don't show the drop down // as a child folder can't have itself as it's own parent. } if(@Model.AllFolders.Count &gt; 1) { &lt;div class="editor-label"&gt; Choose a parent folder (optional) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.ParentFolderId, new SelectList(Model.AllFolders, "Id", "Title")) &lt;/div&gt; } &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p>I've ommitted my view, but this is what my form looks like when rendered in the browser. The form looks good from what I can see?</p> <pre><code> &lt;form Id="CreateFolder" action="/SlapDaBass/Portfolio/EditFolder/1" enctype="multipart/form-data" method="post"&gt; &lt;input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="1" /&gt; &lt;input id="CoverImageFileName" name="CoverImageFileName" type="hidden" value="" /&gt; &lt;input id="ParentFolderId" name="ParentFolderId" type="hidden" value="" /&gt; &lt;div class="editor-label"&gt; &lt;label for="Title"&gt;Title&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input class="text-box single-line" data-val="true" data-val-required="The Title field is required." id="Title" name="Title" type="text" value="Test" /&gt; &lt;span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;label for="UploadedFile"&gt;UploadedFile&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input type="file" name="UploadedFile"/&gt; &lt;span class="field-validation-valid" data-valmsg-for="UploadedFile" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; Choose a parent folder (optional) &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;select id="ParentFolderId" name="ParentFolderId"&gt; &lt;option value="1"&gt;Test&lt;/option&gt; &lt;option value="2"&gt;Test 2&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/form&gt; </code></pre> <p>And this is the controller action:</p> <pre><code>[HttpPost] public ActionResult EditFolder(int id, ImageGalleryFolderViewModel model) { if (ModelState.IsValid) { Services.PortfolioService.UpdateFolder(model.MapToDomainModel(), model.UploadedFile); return Home; } return View(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    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