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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:23:17+00:00 2026-05-23T21:23:17+00:00

I’m trying to combine the Address and Country Create Views into a single view.

  • 0

I’m trying to combine the Address and Country Create Views into a single view. I’m using MVC 3 with Repository Scaffolding & ASPX Views. I currently have a Country drop-down list that is already populated, and am trying to add this to the Address Create View. I have the Edit View working just fine. However when I try to create a new Address it adds a new Country with a blank name even though I choose a Country in the drop down list. I feel like I’m missing something very fundamental because this should be easy.

POCO Classes

public class Address
{
    public int ID { get; set; }
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    [ForeignKey("Country")]
    public int CountryID { get; set; }
    public Country Country { get; set; }
}

public class Country
{
    public int ID { get; set; }
    [Display(Name = "Country"), MaxLength(50)]
    public string Name { get; set; }
}

Address Controller

public class AddressController : Controller
{
    private readonly IAddressRepository addressRepository;
    private readonly ICountryRepository countryRepository;

    // If you are using Dependency Injection, you can delete the following constructor
    public AddressController() : this(new AddressRepository(), new CountryRepository())
    {
    }

    public AddressController(IAddressRepository addressRepository, ICountryRepository countryRepository)
    {
        this.addressRepository = addressRepository;
        this.countryRepository = countryRepository;
    }

    //
    // GET: /Address/

    public ViewResult Index()
    {
        return View(addressRepository.All);
    }

    //
    // GET: /Address/Details/5

    public ViewResult Details(int id)
    {
        return View(addressRepository.Find(id));
    }

    //
    // GET: /Address/Create

    public ActionResult Create()
    {
        ViewBag.PossibleCountries = countryRepository.All;
        return View();
    } 

    //
    // POST: /Address/Create

    [HttpPost]
    public ActionResult Create(Address address)
    {
        if (ModelState.IsValid) {
            addressRepository.InsertOrUpdate(address);
            addressRepository.Save();
            return RedirectToAction("Index");
        } else {
            ViewBag.PossibleCountries = countryRepository.All;
            return View();
        }
    }

    //
    // GET: /Address/Edit/5

    public ActionResult Edit(int id)
    {
        ViewBag.PossibleCountries = countryRepository.All;
        return View(addressRepository.Find(id));
    }

    //
    // POST: /Address/Edit/5

    [HttpPost]
    public ActionResult Edit(Address address)
    {
        if (ModelState.IsValid) {
            addressRepository.InsertOrUpdate(address);
            addressRepository.Save();
            return RedirectToAction("Index");
        } else {
            ViewBag.PossibleCountries = countryRepository.All;
            return View();
        }
    }

    //
    // GET: /Address/Delete/5

    public ActionResult Delete(int id)
    {
        return View(addressRepository.Find(id));
    }

    //
    // POST: /Address/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        addressRepository.Delete(id);
        addressRepository.Save();

        return RedirectToAction("Index");
    }
}

Country Controller

public class CountryController : Controller
{
    private readonly ICountryRepository countryRepository;

    // If you are using Dependency Injection, you can delete the following constructor
    public CountryController() : this(new CountryRepository())
    {
    }

    public CountryController(ICountryRepository countryRepository)
    {
        this.countryRepository = countryRepository;
    }

    //
    // GET: /Country/

    public ViewResult Index()
    {
        return View(countryRepository.All);
    }

    //
    // GET: /Country/Details/5

    public ViewResult Details(int id)
    {
        return View(countryRepository.Find(id));
    }

    //
    // GET: /Country/Create

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

    //
    // POST: /Country/Create

    [HttpPost]
    public ActionResult Create(Country country)
    {
        if (ModelState.IsValid) {
            countryRepository.InsertOrUpdate(country);
            countryRepository.Save();
            return RedirectToAction("Index");
        } else {
            return View();
        }
    }

    //
    // GET: /Country/Edit/5

    public ActionResult Edit(int id)
    {
         return View(countryRepository.Find(id));
    }

    //
    // POST: /Country/Edit/5

    [HttpPost]
    public ActionResult Edit(Country country)
    {
        if (ModelState.IsValid) {
            countryRepository.InsertOrUpdate(country);
            countryRepository.Save();
            return RedirectToAction("Index");
        } else {
            return View();
        }
    }

    //
    // GET: /Country/Delete/5

    public ActionResult Delete(int id)
    {
        return View(countryRepository.Find(id));
    }

    //
    // POST: /Country/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        countryRepository.Delete(id);
        countryRepository.Save();

        return RedirectToAction("Index");
    }
}

Address Repository

public class AddressRepository : IAddressRepository
{
    AddressTestContext context = new AddressTestContext();

    public IQueryable<Address> All
    {
        get { return context.Addresses; }
    }

    public IQueryable<Address> AllIncluding(params Expression<Func<Address, object>>[] includeProperties)
    {
        IQueryable<Address> query = context.Addresses;
        foreach (var includeProperty in includeProperties) {
            query = query.Include(includeProperty);
        }
        return query;
    }

    public Address Find(int id)
    {
        return context.Addresses.Find(id);
    }

    public void InsertOrUpdate(Address address)
    {
        if (address.ID == default(int)) {
            // New entity
            context.Addresses.Add(address);
        } else {
            // Existing entity
            address.CountryID = address.Country.ID;
            context.Entry(address).State = EntityState.Modified;
        }
    }

    public void Delete(int id)
    {
        var address = context.Addresses.Find(id);
        context.Addresses.Remove(address);
    }

    public void Save()
    {
        context.SaveChanges();
    }
}

public interface IAddressRepository
{
    IQueryable<Address> All { get; }
    IQueryable<Address> AllIncluding(params Expression<Func<Address, object>>[] includeProperties);
    Address Find(int id);
    void InsertOrUpdate(Address address);
    void Delete(int id);
    void Save();
}

Address CreateOrEdit.ascx View

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AddressTest.Models.Address>" %>

<div class="editor-label">
    <%: Html.LabelFor(model => model.Street1) %>
</div>
<div class="editor-field">
    <%: Html.EditorFor(model => model.Street1) %>
    <%: Html.ValidationMessageFor(model => model.Street1) %>
</div>

<div class="editor-label">
    <%: Html.LabelFor(model => model.Street2) %>
</div>
<div class="editor-field">
    <%: Html.EditorFor(model => model.Street2) %>
    <%: Html.ValidationMessageFor(model => model.Street2) %>
</div>

<div class="editor-label">
    <%: Html.LabelFor(model => model.City) %>
</div>
<div class="editor-field">
    <%: Html.EditorFor(model => model.City) %>
    <%: Html.ValidationMessageFor(model => model.City) %>
</div>

<div class="editor-label">
    <%: Html.LabelFor(model => model.State) %>
</div>
<div class="editor-field">
    <%: Html.EditorFor(model => model.State) %>
    <%: Html.ValidationMessageFor(model => model.State) %>
</div>

<div class="editor-label">
    <%: Html.LabelFor(model => model.PostalCode) %>
</div>
<div class="editor-field">
    <%: Html.EditorFor(model => model.PostalCode) %>
    <%: Html.ValidationMessageFor(model => model.PostalCode) %>
</div>

<div class="editor-label">
    <%: Html.LabelFor(model => model.Country) %>
</div>
<div class="editor-field">
    <%: Html.DropDownListFor(model => model.Country.ID, ((IEnumerable<AddressTest.Models.Country>)ViewBag.PossibleCountries).Select(option => new SelectListItem {
        Text = (option == null ? "None" : option.Name), 
        Value = option.ID.ToString(),
        Selected = (Model != null) && (option.ID == Model.CountryID)
    }), "Choose...") %>
    <%: Html.ValidationMessageFor(model => model.Country.ID) %>
</div>
  • 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-23T21:23:17+00:00Added an answer on May 23, 2026 at 9:23 pm

    Create the drop down list for the scalar property CountryID instead of Country.ID

    <div class="editor-field">
        <%: Html.DropDownListFor(model => model.CountryID, new SelectList((IEnumerable<AddressTest.Models.Country>)ViewBag.PossibleCountries, "ID", "Name"), "Choose...") %>
        <%: Html.ValidationMessageFor(model => model.CountryID) %>
    </div>
    

    I would modify the Address POCO to make CountryID nullable and apply Required attribute

    public class Address
    {
        public int ID { get; set; }
        public string Street1 { get; set; }
        public string Street2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        [ForeignKey("Country")]
        [Required]
        public int? CountryID { get; set; }
        public Country Country { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
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 have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
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
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.