Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Model property is not being set in partial view called by html.action
    primarykey
    data
    text
    <p>I have an issue with my app, ill try explain by example, I have a form, this form exist of several textboxes and dropdown lists. For reusability I merged 3 drop down lists into a partial view, this partial view i load with @Html.Action, this works ok, when i start the form i see everything appears as it should be, although I dont know why but those required drop down lists directly are shown with red start and said it is required fields.</p> <p>But when i fill everything in, and i select the values from the drop down lists, and i click on OK, the values from drop down lists are NULL.</p> <p>I think it will be more comprehendable with a code example:</p> <p>Main model:</p> <pre><code>public class FormModel { [Required] public string UserName { get; set; } [Required] [Display(Name = "Birthdate")] public DateTime? Birthdate { get; set; } //This is what i'm talking about, that is not being set, the location public Location Location { get; set; } } </code></pre> <p>Here is the location class, which is then passed to the partial view and normally i think should be set, it looks like this:</p> <pre><code>public class Location { [Required] [Display(Name = "Country")] public string CountryId { get; set; } [Required] [Display(Name = "Region")] public string RegionId { get; set; } [Required] [Display(Name = "City")] public string CityId { get; set; } } </code></pre> <p>Now we have a partial view for the location:</p> <pre><code>@model TestWebsite.Models.Location &lt;tr&gt; &lt;td class="editor-label"&gt; @Html.LabelFor(m =&gt; m.CountryId) &lt;/td&gt; &lt;td class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.CountryId, Model.Countries, "---select--", null) &lt;/td&gt; &lt;td&gt; @Html.ValidationMessageFor(m =&gt; m.CountryId) &lt;/td&gt; &lt;/tr&gt; &lt;!-- Region --&gt; &lt;tr&gt; &lt;td class="editor-label"&gt; @Html.LabelFor(m =&gt; m.RegionId) &lt;/td&gt; &lt;td class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.RegionId, Enumerable.Empty&lt;SelectListItem&gt;(), "---select--", null) &lt;/td&gt; &lt;td&gt; @Html.ValidationMessageFor(m =&gt; m.RegionId) &lt;/td&gt; &lt;/tr&gt; &lt;!-- City --&gt; &lt;tr&gt; &lt;td class="editor-label"&gt; @Html.LabelFor(m =&gt; m.CityId) &lt;/td&gt; &lt;td class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.CityId, Enumerable.Empty&lt;SelectListItem&gt;(), "---select--", null) &lt;/td&gt; &lt;td&gt; @Html.ValidationMessageFor(m =&gt; m.CityId) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>Then we call this partial view in the forms like this:</p> <pre><code>@Html.Action("LocationGroup", "Account", Model.Location) </code></pre> <p>And finally in the controller:</p> <pre><code> [ChildActionOnly] public ActionResult LocationGroup(Location model) { model.Countries = GetCountries(); return PartialView("_LocationView", model); } </code></pre> <p>I know it is a lot of code, but i hope you could help me ...</p>
    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.
 

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