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 7993413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:50:37+00:00 2026-06-04T13:50:37+00:00

I have an MVC3 razor project, which I am trying to build using multiple

  • 0

I have an MVC3 razor project, which I am trying to build using multiple reusable elements. I have many model classes like the following

public class ProductCreateModel
    {
        [Required]
        [Display(Name = "Org")]
        [UIHint("MyOrgsDropDown")]
        public string orgName { get; set; }

        [Required(ErrorMessage = "Select an account")]
        [Display(Name = "Account")]
        [UIHint("MyAccountsDropDown")]
        [AdditionalMetadata("orgFieldId", "orgName")]
        [AdditionalMetadata("fieldId", "accountName")]
        public string accountName { get; set; }
    }

There are other fields which I am omitting for this example

MyAccountsDropDown.cshtml looks like this:

@{  
var values = ViewData.ModelMetadata.AdditionalValues;
string orgFieldId = (string)values["orgFieldId"];
string fieldId = (string)values["fieldId"];
}
<script type="text/javascript">
$(document).ready(function () {
    $("#@orgFieldId").change(function () {
        var idProd = $(this).val();
        $.getJSON("/JsonService/GetAccounts", { orgId: idProd },
            function (myData) {
                var select = $("#@fieldId");
                select.empty(); 
                $.each(myData, function (index, itemData) {
                    select.append($('<option/>', {
                        value: itemData.Value,
                        text: itemData.Text
                    }));
                });
            });
    });
});
</script>


@Html.DropDownList("", new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "---Select---", new { id = fieldId })

My Controller methods look like this:

    [HttpGet]
    public ActionResult AddProduct()
    {
        return PartialView("_AddProduct");
    }

    [HttpPost]
    public ActionResult AddProduct(ProductCreateModel model)
    {
        if (!ModelState.IsValid)
        {
            return PartialView("_AddProduct", model);
        }

        //do some stuff

        return PartialView("_AddProduct");
    }

So when the view opens (as a jQuery dialog box), I have the form all set. If I select an orgName, it pulls the accountName from the database (cascading). Now since I am using model validation, when I DO NOT fill in all the required fields, and press submit, it shows me the validation messages. It also shows me the pre-filled values of the fields I had already entered before submit. What is removes are the cascaded drop down values. So it continues to show the selected orgName, but the accountName drop down loses the previously filled values from the database.

How can I maintain the already pulled SelectItems from before the submit was clicked

  • 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-06-04T13:50:39+00:00Added an answer on June 4, 2026 at 1:50 pm

    Change the code in your javascript section to:

      $("#@orgFieldId").change(function () {
          var idProd = $(this).val();
          $.getJSON("/JsonService/GetAccounts", { orgId: idProd },
              function (myData) {
                  var select = $("#@fieldId");
                  select.empty(); 
                  $.each(myData, function (index, itemData) {
                      select.append($('<option/>', {
                          value: itemData.Value,
                          text: itemData.Text
                      }));
                  });
              });
      });
    
      $(document).ready(function () {
        $("#@orgFieldId").change();
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string created using asp.net MVC3 Razor block like this: @{ var
I am working on a project which adopted ASP.NET MVC3(Razor) tech. Now, I have
I have a Razor MVC3 project which has two user records in a form,
I have a simple PartialView setup in my MVC3 Project using the Razor View
I have something like this in Site.Master in an asp.net mvc3 (not razor) project:
I am using JQuery and MVC3 razor on a project and I have a
ASP.NET MVC3 Razor project Trying to implement OpenID using http://weblogs.asp.net/haithamkhedre/archive/2011/03/13/openid-authentication-with-asp-net-mvc3-dotnetopenauth-and-openid-selector.aspx I've edited the openid-en.js
I am working on an MVC3 application using the Razor syntax and I have
I had created a webpage using asp.net mvc3 razor. I have two master pages
I'm using MVC3 razor and in Views folder I have 4 folder: Shared _Layout.cshtml

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.