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

  • SEARCH
  • Home
  • 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 6919983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:05:26+00:00 2026-05-27T10:05:26+00:00

Relatively new to MVC and trying to get a cascading dropdown list working for

  • 0

Relatively new to MVC and trying to get a cascading dropdown list working for train times.

After looking at a lot of posts, people say that you should stay away from ViewBag/ViewData and instead focus on ViewModels, but I just can’t seem to get my head round it, and it’s driving me up the wall. Any tutorial seems to be either to complex or too easy and the whole viewModel idea just hasn’t clicked with me yet.

So here is my scenario: I have an admin system where staff can add individual train journeys. For each train time, I have an input form where the user can choose a Company, and from there, I’d like the dropdownlist underneath to populate with a list of journey numbers, which indicate routes. Once they have chosen a number, they can carry on with the rest of the form, which is quite large, including time of travel, facilities on the train etc.

I’ve created a viewmodel like so:

public class JourneyNumbersViewModel
    {
        private List<SelectListItem> _operators = new List<SelectListItem>();
        private List<SelectListItem> _journeys= new List<SelectListItem>();

        [Required(ErrorMessage = "Please select an operator")]
        public string SelectedOperator { get; set; }
        [Required(ErrorMessage = "Please select a journey")]
        public string SelectedJourney { get; set; }

        public List<SelectListItem> Journeys
        {
            get { return _journeys; }
        }
        public List<SelectListItem> Operators
        {
            get
            {
                foreach(Operator a in Planner.Repository.OperatorRepository.GetOperatorList())
                {
                    _operators.Add(new SelectListItem() { Text = a.OperatorName, Value = a.OperatorID.ToString() });
                }
                return _operators;
            }
        }
    }

In my controller, I have this for the Create view:

    public ActionResult Create()
    {
        return View(new JourneyNumbersViewModel());
    }

And this is where it isn’t really working for me – if I change my model at the top of the Create view to: @model Planner.ViewModels.JourneyNumbersViewModel, then the rest of my form throws errors as the model is no longer correct for the rest of the form. Is this the way it is supposed to work – what if you need to reference multiple view models with a single view?

I know this is a simple thing and as soon as it clicks I’ll wonder how on earth I could have struggled with it in the first place, but if anyone can point out where I’m going wrong, I’d be very grateful.

  • 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-27T10:05:27+00:00Added an answer on May 27, 2026 at 10:05 am

    I have done something similar. Here is some of the code (apologies upfront for this being quite long, but I wanted to make sure you could re-create this on your side):

    View looks like this:

    using Cascading.Models
    @model CascadingModel
    
    
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Cascading Forms</h2>
    <table>
    
    @using(Html.BeginForm("Index", "Home"))
    {
    <tr>
        <td>@Html.LabelFor(m=>m.CategoryId)</td>
        <td>@Html.DropDownListFor(m => m.CategoryId, new SelectList(Model.Categories, "Id", "Name"), string.Empty)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(m=>m.ProductId)</td>
        <td>@Html.CascadingDropDownListFor(m => m.ProductId, new SelectList(Model.Products, "Id", "Name"), string.Empty, null, "CategoryId", "Home/CategorySelected")</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="Go"/></td>
    </tr>
    }
    </table>
    

    the Model looks as follows:

    public class CascadingModel
    {
        public int CategoryId { get; set; }
        public List<Category> Categories { get; set; }
        public int ProductId { get; set; }
        public List<Product> Products { get; set; }
    }
    

    the real “clever” part of the system is the Html.CascadingDropDownListFor which looks as follows:

    public static class MvcHtmlExtensions
    {
        public static MvcHtmlString CascadingDropDownListFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            IEnumerable<SelectListItem> selectList,
            string optionLabel,
            IDictionary<string, Object> htmlAttributes,
            string parentControlName,
            string childListUrl
            )
        {
            var memberName = GetMemberInfo(expression).Member.Name;
    
            MvcHtmlString returnHtml = Html.SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel, htmlAttributes);
    
            var returnString = MvcHtmlString.Create(returnHtml.ToString() + 
                        @"<script type=""text/javascript"">
                            $(document).ready(function () {
                                $(""#<<parentControlName>>"").change(function () { 
                                    var postData = { <<parentControlName>>: $(""#<<parentControlName>>"").val() };
                                    $.post('<<childListUrl>>', postData, function (data) {
                                        var options = """";
                                        $.each(data, function (index) {
                                            options += ""<option value='"" + data[index].Id + ""'>"" + data[index].Name + ""</option>"";
                                        });
                                        $(""#<<memberName>>"").html(options);
                                    })
                                    .error(function (jqXHR, textStatus, errorThrown) { alert(jqXHR.responseText); });
                                });
                            });
                         </script>"
                        .Replace("<<parentControlName>>", parentControlName)
                        .Replace("<<childListUrl>>", childListUrl)
                        .Replace("<<memberName>>", memberName));
    
            return returnString;
    
        }
    
        private static MemberExpression GetMemberInfo(Expression method)
        {
            LambdaExpression lambda = method as LambdaExpression;
            if (lambda == null)
                throw new ArgumentNullException("method");
    
            MemberExpression memberExpr = null;
    
            if (lambda.Body.NodeType == ExpressionType.Convert)
            {
                memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
            }
            else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
            {
                memberExpr = lambda.Body as MemberExpression;
            }
    
            if (memberExpr == null)
                throw new ArgumentException("method");
    
            return memberExpr;
        }
    }
    

    Controller Logic for those looking for it:

    public ActionResult CategoriesAndProducts()
    {
        var viewModel = new CategoriesAndProductsViewModel();
        viewModel.Categories = FetchCategoriesFromDataBase();
        viewModel.Products = FetchProductsFromDataBase();
        viewModel.CategoryId = viewModel.Categories[0].CategoryId;
        viewModel.ProductId = viewModel.Products.Where(p => p.CategoryId).FirstOrDefault().ProductId;
        return View(viewModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an MVC ASP .NET application. I am relatively new to
I am relatively new to the whole MVC way of looking at things. I
I am relatively new to ASP.NET MVC, and am very impressed with the clarity
Relatively new to rails and trying to model a very simple family tree with
I'm slowly getting into MVC, but it's still relatively new to me. From what
I'm relatively new to MVC and the Zend Framework. That being said, I feel
I'm relatively new to ASP.NET MVC so I'm having a little trouble with this
I'm relatively new to MVC and EF Code first and cannot find a solution
I am relatively new to MVC, so this is likely a novice question. I
When using the [relatively new] MVC bits in Sencha Touch I find that 90%

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.