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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:19:09+00:00 2026-05-23T07:19:09+00:00

Hi guys in new with asp mvc, so i having problem with the DropDown

  • 0

Hi guys in new with asp mvc, so i having problem with the DropDown in edit view:

The ViewData item that has the key 'ProvinciaID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

Provincia Model:

public class Provincia
    {
        public int ProvinciaID { get; set; }
        [DisplayName("Provincia")]
        [Required(ErrorMessage = "Provincia es requerida")]
        public string ProvinciaNombre { get; set; }
    }

Registro Model:

public class Registro
    {
        public int ID { get; set; }
        ....

        [DisplayName("Provincia")]
        [Required]
        public int ProvinciaID { get; set; }
        public List<Provincia> rProvincia { get; set; }
        ....
}

Controller

public ActionResult Edit(int id)
        {
            Registro registro = db.Registros.Find(id);

            ViewBag.provincias = new SelectList(db.Provincias, "ProvinciaID", "ProvinciaNombre", registro.ProvinciaID);            

            return View(registro);
        }

View

<div class="editor-label">
            @Html.LabelFor(model => model.ProvinciaID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("ProvinciaID", (IEnumerable<SelectListItem>)ViewData["provincias"]))
            @Html.ValidationMessageFor(model => model.ProvinciaID)
        </div>

Any idea?

Thanks guys

  • 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-23T07:19:10+00:00Added an answer on May 23, 2026 at 7:19 am

    The problem is that you are using this Edit view (the one you showed in your question) in some other controller action and this other controller action doesn’t set ViewBag.provincias. You must always set ViewBag.provincias if you want to use this view. I suppose that this other controller action is the one you are POSTing to the form:

    [HttpPost]
    public ActionResult Edit(Registro registro)
    {
        if (!ModelState.IsValid)
        {
            // I guess that here you are trying to redisplay the Edit view
            // but you forgot to set ViewBag.provincias as you did in the
            // GET Edit action and an exception is thrown because the Edit view
            // always expects ViewBag.provincias to be set
    
            // So set it before returning to the same view:
    
            ViewBag.provincias = new SelectList(db.Provincias, "ProvinciaID", "ProvinciaNombre", registro.ProvinciaID);
    
            return View(registro);
        }
        return RedirectToAction("Success");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new at using the ASP.NET MVC framework and was hoping that
Hai guys, I started a new asp.net mvc application and i followed the steps
Hi guys i know this is a known problem in ASP.NET MVC, basically what
Hi Guys new to this site but a big fan. Right the problem. It's
Guys, is it possible to get physical path to asp.net mvc 2 application inside
Hi guys i am new to Sitecore and asp.net and iis. I have installed
I'm very new to ASP.net MVC, so please be as descriptive as possible in
Im fairly new to Asp.Net arena here guys,so please bear with me if say
I have written an ASP.NET MVC application that allows the user to specify their
I have to admit in advance that I'm quite new to MVC, I've been

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.