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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:53:16+00:00 2026-06-16T21:53:16+00:00

I’m trying to query List from database which will be shown in dropdownlistfor in

  • 0

I’m trying to query List from database which will be shown in dropdownlistfor in view, and chosen result will be post.

The problem is I’m using two models in view I tried more options

I’m lost with this, I don’t know how to post it. Data are shown in listbox without problem. I’m getting this error when I try to post:

“There is no ViewData item of type ‘IEnumerable’ that
has the key ‘Item2.idcity’.” System.Exception
{System.InvalidOperationException}

CityModel

public class CityModel
{
    public int idcity { get; set; }
    [Required]
    public string cityName { get; set; }

    public IEnumerable<CityModel> citys { get; set; }
}

HospitalModel

public class HospitalShowModels
{
    [Required]
    public string hospitalName { get; set; }
    [Required]
    public string cityName { get; set; }
}

HospitalControler.Create()

public ActionResult Create()
{
        ViewBag.cityModel = new SelectList(DataAccess.DAL.showCity(), "idcity", "cityName");
        var tuple = new Tuple<DataAccess.HospitalShowModels, DataAccess.CityModel>(new DataAccess.HospitalShowModels(), new DataAccess.CityModel());
        return View(tuple);
}

[HttpPost]
public ActionResult Create(DataAccess.HospitalShowModels model, DataAccess.CityModel model1)
{
        if (ModelState.IsValid) {
            DataAccess.DAL.insertHospital(model.hospitalName, model1.cityName);     
        }else{
            ModelState.AddModelError("","Invalid options");
        }
        return View();
}

View.Create

@model Tuple<ProjektZaja.DataAccess.HospitalShowModels,ProjektZaja.DataAccess.CityModel>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Create Hospital</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Item1.hospitalName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Item1.hospitalName)
        @Html.ValidationMessageFor(model => model.Item1.hospitalName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Item1.cityName)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => Model.Item2.idcity,(IEnumerable<SelectListItem>)ViewBag.cityModel)
        @Html.ValidationMessageFor(model1 => Model.Item2.idcity)
  • 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-16T21:53:17+00:00Added an answer on June 16, 2026 at 9:53 pm

    You can’t strongly type a view to multiple models. You need to create a view model which will simply be a composite class containing each of the other two classes, and strongly type the view to that view model.

    public class CreateViewModel
    {
      public CityModel CityModel {get; set;}
      public HospitalShowModel HospitalShowModel {get; set;}
    }
    

    Then strongly type your view to the view model

    @ model Full.Path.To.CreateViewModel
    

    And access the models properties appropriately

    Model.CityModel.cityName //etcetera...
    

    And bind it in the post method

    [HttpPost]
    public ActionResult Create(CreateViewModel viewModel)
    {
      //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am currently running into a problem where an element is coming back from

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.