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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:29:23+00:00 2026-06-08T21:29:23+00:00

I’m trying to create a basic registration form that takes in an address with

  • 0

I’m trying to create a basic registration form that takes in an address with a state from a drop down list. I can’t seem to extract the data from the drop down into my model that contains a state object. I’ve tried creating a custom model binder as shown below. When I debug the ModelState is valid, but the state is always null.

Models:

public class AccountInfo 
{
    public virtual Guid accoundID { get; set; }
    public virtual string city { get; set; }
    public virtual string email_address { get; set; }
    public virtual string fax_number { get; set; }
    public virtual string first_name { get; set; }
    public virtual string last_name { get; set; }
    public virtual string mailing_address { get; set; }
    public virtual string phone_number { get; set; }
    public virtual State state { get; set; }
    public virtual string zip_code { get; set; }
}
public class State
{
    public virtual int ID { get; set; }
    public virtual string text { get; set; }
    public virtual string value { get; set; }
}

Model Binder:

 public class StateModelBinder : IModelBinder
{
    private RepositoryDB Database;
    public object BindModel(
        ControllerContext controllerContext,
        ModelBindingContext bindingContext
        )
    {
        var key = bindingContext.ModelName + ".state";
        var value = bindingContext.ValueProvider.GetValue(key);
        if (value == null)
        {
            return null;
        }
        var result = new State();
        Database = new RepositoryDB(ConfigurationManager.
            ConnectionStrings["ConnectionString"].ConnectionString);

        try
        {
            var query = from s in Database.States
                        where s.value == value.ToString()
                        select new State()
                        {
                            text = s.text,
                            ID = s.ID,
                            value = s.value
                        };
            result = (State)query.ToList().ElementAt(0);        
        }
        catch (Exception ex)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
            bindingContext.ModelState.SetModelValue(key, value);
        }


        return result;
    }

Controller:

[HttpPost]
    public ActionResult Index(AccountInfo accountModel)
    {
        try
        {
            if (ModelState.IsValid)
            {
                Database.Account.Add(accountModel);
                Database.SaveChanges();

                return Redirect(Url.Action("Success"));
            }
        }
        catch (Exception ex)
        {
            ModelState.AddModelError(String.Empty, ex);
        }
        // invalid info - return with error message         
        //repop select lists
        GetStates();
        return View(accountModel);
    }
public void GetStates()
    {
        IEnumerable<SelectListItem> states = from s in Database.States
                                                select new SelectListItem
                                                {
                                                    Text = s.text,
                                                    Value = s.value
                                                };

        ViewBag.state = states.ToList();
    }

View:

@Html.LabelFor(model => model.state, "*State")
@Html.DropDownListFor(model => model.state, (IEnumerable<SelectListItem>)ViewBag.state)
  • 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-08T21:29:25+00:00Added an answer on June 8, 2026 at 9:29 pm

    Here is your problem:

    @Html.DropDownListFor(model => model.state, (IEnumerable<SelectListItem>)ViewBag.state)
    

    Binding to model.State cannot work because that is a complex type:

    public class State
    {
        public virtual int ID { get; set; }
        public virtual string text { get; set; }
        public virtual string value { get; set; }
    }
    

    If you think about it the only thing the form is going to post back is the value of the selected drop down item like “MN” (Minnesota) and not a full class. (You haven’t serialized a “State” class to each drop down list item’s value, just the primary key of each state hopefully) You need to make your state on the model be a string or int depending on what type that primary key is of your state like so:

    public class AccountInfo 
    {
        //...
        public virtual string/int State { get; set; }
        //...
    }
    

    From there you can re-hydrate your State object if you’d like from the unique identifier of a state (which should be the value that is posted back to the server as a string or int for example)

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.