Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 534133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:34:48+00:00 2026-05-13T09:34:48+00:00

I have a viewmodel class for a create view that consists of 2 properties

  • 0

I have a viewmodel class for a create view that consists of 2 properties different classes, and a list of selectitems (for dropdownlist). The dropdown list and the fields for one of the classes are always shown in the view,but the fields for the 2nd class are only shown if a specific option of the list is selected. (via a jQuery load and a controller that returns a partial view, strongly typed for the 2nd class)

The fields in the view for the second class are some normal input=text fields and a dropdown list.

When I submit the form, all of the data will be properly binded, except for the values for the list inside the second class.

The problem is that if there is a validation error, and I return the view with the model (to show the errors) then the dropdownlist (the one for the second class) is empty, and since there is no way to choose from it, then the submit will always have isValid=false :S

This is an example of how I have setup things:

I currently have a view model that is looks like the following:

public class ViewModel
{
   public GenericData genericData{get;set;}
   public SpecializedData specializedData {get;set;}
   public List<SelectListItem> types { get; set; }
}

public class GenericData
{
   public Type type {get;set;}
   public String name {get;set;}
   public String description{get;set;}
}
public class SpecializedData
{
   public String field{get;set;}
   public int foreignKey{get;set;}
   public List<SelectListItem> listOfForeignKeys{ get; set; }
}

The view is strongly typed for ViewModel and is setup this way:

<p>
   <label for="genericData.type"> Type:  </label>
        <%=Html.DropDownList("genericData.type",Model.types, "Choose a type") %>
</p>
<p>
    <label for="genericData.name"> Name:</label>
    <%: Html.TextBox("genericData.name")%>
    <%= Html.ValidationMessage("genericData.name", "*")%>
</p>
//Same thing for Description as for name

<div id="SpecializedFields">
           //This will display the specialized info it was already captured before.
           <% if (Model.SpecializedData!= null && Model.GenericData.Type == "Specialized")
           {
               Html.RenderPartial("SpecializedInfoView", Model.specializedData);
           } %>
</div>
<p>
     <input type="submit" value="Create" />
</p>

$(document).ready(function () {
        $("#CatalogItem_Type").change(function () {

            var typeVal = $("#GenericData_type").val();
            if (typeVal == "specialized") {
                $("#SpecializedFields").load("/MyController/SpecializedFields");
            }

the controller will do something like this

public ActionResult SpecializedFields()
{
     List<SelectListItem> foreignIds = getForeignIdsFromDataBase();
     SpecializedData model = new SpecializedData ();
     model.listOfForeignIds = foreignIds;
     return PartialView("SpecializedInfoView",model);
}

Finally, the SpecialezedInfoView is a strongly typed view for SpecializedData and is similar to the generic one, it goes like this:

<p>
   <label for="specializedData.foreignKey"> Key:  </label>    <%=Html.DropDownList("specializedData.foreignKey",Model.listOfForeignKeys, "Choose key") %>
</p>
<p>
    <label for="SpecializedData.field"> Field:</label>
    <%: Html.TextBox("specializedData.field")%>
    <%= Html.ValidationMessage("specializedData.field", "*")%>
</p>

When I click submit, if its not a specialized item, i will get a null for the specializedData property, which is ok. If it is a specialized item, then it will not be null, I’ll get all the properties from the genericData and I’ll get the specializedData.field and specializedData.foreignKey value too, but the specialized.listOfForeignKeys will be a NULL which means that if there is a validation error and i return the view with the current model, then everything will have the previously entered values, BUT the DropDownList with the list of foreign keys will be empty, and there is no way to choose an id now.

I know i could check to see if the specializedData is not null in the method that receives the post, and re-generate the list of keys and assign it to the current model (since it is the only data it is missing), but that would mean having to do extra DB calls, instead of the info being there with the model!

I know it is a long question, but I wanted to be explicit on how I am setting up things, do somebody know why is the listOfForeignKeys not being passed back the the model after postback!?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:34:48+00:00Added an answer on May 13, 2026 at 9:34 am

    I figured out… the thing is that values for DropDownLists are NOT sent back to the model when you do I binding… however, since the binding automatically calls the constructor of the viewmodel the List<SelectItemList> Types was being filled there on not really because of the binding. The second list is not built in the constructor, thus it came as null. I added the query to get the information for the list in the SpecializedData constructor, and now its working..

    Still it kind of sucks that when the user hits post there will be an extra call to the DB, and more times so if there are validation errors. Maybe I’ll try to do a cache to store the query results, but for the time being, this is enough.

    Thanks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have ViewModel class MyViewModel like: public class MyViewModel : ViewModelBase { private
I have a ViewModel: public class Page { public int Id { get; set;
I have the following nested viewmodel class... public class CustomerModel { public string name;
I have a view model that is used as the data source for my
I have a shell which looks like toolbar and defines my main region (a
I like the way model binding in django works: you send stuff that you
I'm new to asp.net mvc, but I've thoroughly searched on this topic everywhere. I've
I'm not even quite sure where to start explaining this problem. I've been working
I want a TreeView with checkboxes and I'm trying to follow this tutorial .
Ok, I recently implemented a background worker to perform saving and loading of data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.