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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:07:39+00:00 2026-05-25T20:07:39+00:00

I hope someone can help with this one. I have three Model classes like

  • 0

I hope someone can help with this one. I have three Model classes like this:

public class Provider
{
    public Guid ProviderId { get; set; }
    public string Name { get; set; }

    public Guid LocationId { get; set; }

    public virtual Location Location { get; set; }
}

public class Location
{
    public Guid LocationId { get; set; }
    public string NameOrCode { get; set; }
    public string Description { get; set; }
    public string StreetNumber { get; set; }
    public string StreetAddress1 { get; set; }
    public string StreetAddress2 { get; set; }
    public string City { get; set; }

    public int? StateId { get; set; }

    public string Zip { get; set; }
    public string ContactPhone { get; set; }

    public virtual State State { get; set; }
}

public class State
{
    public int StateId { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
}

As you can see, a Provider has a Location (separate class for reuse elsewhere), and a Location has a State (which is null until selected).

My Controller looks like this for my Create methods:

public class ProviderController : BaseController
{
    private SetupContext db = new SetupContext();

    // other CRUD methods ...

    //
    // GET: /Provider/Create

    public ActionResult Create()
    {
        Location location = new Location() 
        { 
            LocationId = Guid.NewGuid(), 
            NameOrCode = Resources.BillingLocation,
            Description = Resources.BillingLocationDescription
        };
        Provider provider = new Provider()
        {
            ProviderId = Guid.NewGuid(),
            LocationId = location.LocationId,
            Location = location
        };
        ViewBag.StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);

        return View(provider);
    } 

    //
    // POST: /Provider/Create

    [HttpPost]
    public ActionResult Create(Provider provider)
    {
        if (ModelState.IsValid)
        {
            db.Locations.Add(provider.Location);
            db.Providers.Add(provider);
            db.SaveChanges();

            return RedirectToAction("Index");  
        }

        ViewBag.StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);
        return View(provider);
    }

    // other CRUD methods ... 
}

Finally, my View looks like this:

<div class="editor-label">
    @Html.LabelFor(model => model.Location.StateId, @Resources.Location_State_Display_Name)
</div>
<div class="editor-field">
    @Html.DropDownList("StateId", @Resources.ChooseFromSelectPrompt)
    @Html.ValidationMessageFor(model => model.Location.StateId)
</div>

My problem is that the state the user selects in the DropDownList never gets set on my Model on the Create POST. I have similar code in my Edit View and the state is populated correctly in that View (that is, the state associated with an existing Provider.Location shows selected in the DropDownList for the user to edit if desire), but in both the Create and the Edit Views the selection made by the user is never registered in my Model (specifically the Provider.Location.StateId) coming in from the POST.

Looking at the HTML produced I see this:

<div class="editor-label">
    <label for="Location_StateId">State/Territory</label>
</div>
<div class="editor-field">
    <select id="StateId" name="StateId"><option value="">[Choose]</option>
        <option value="1">Alabama</option>
        <option value="2">Alaska</option>
        <!-- more options ... -->
    </select>
    <span class="field-validation-valid" data-valmsg-for="Location.StateId" data-valmsg-replace="true"></span>
</div>

I suspect I need to somehow convey the Location.StateId relationship instead of just StateId as I see above but I can’t figure out the correct syntax to do that. I’ve tried changing my ViewBag dynamic property to Location_StateId like this:

ViewBag.Location_StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);

And the DropDownList in my View like this:

@Html.DropDownList("Location_StateId", @Resources.ChooseFromSelectPrompt)

I figured then perhaps that notation would work because the label beside my DropDownList was rendered as:

<div class="editor-label">
    <label for="Location_StateId">State/Territory</label>
</div>

This attempt did not work. Can you help me out?

Thanks in advance.

  • 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-25T20:07:40+00:00Added an answer on May 25, 2026 at 8:07 pm
    @Html.DropDownList("Location.StateId", @Resources.ChooseFromSelectPrompt)
    

    Also the following line doesn’t do anything useful:

    ViewBag.StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);
    

    You are assigning a SelectList to something that is supposed to be a scalar property. You probably wanted to pass the collection as ViewBag:

    ViewBag.States = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);
    

    and then in the view:

    @Html.DropDownList("Location.StateId", (SelectList)ViewBag.States)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope someone can help. I have a link like this: <a id='testOne' onclick=doTest('one');
I hope someone can help me with this one, I suspect I am doing
I hope someone can help me with this, as I'm unable to find the
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I hope there's someone who can help me suggest a suitable data model to
I hope someone can help with this please. I am trying to query an
my SQL skills aren't strong enough to figure this out, hope someone can help.
Hey hope someone can help as I am at my wits end with this!?
this my first question on here. I hope someone can help. It is to
I hope someone can help me with this. I am having some trouble figuring

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.