The model passed into the dictionary is of type 1 but this dictionary requires a model of type 2

2.3k views Asked by At

This is my whole error:

The model item passed into the dictionary is of type 'System.Data.Entity.DbSet`1[application.Models.model]', but this dictionary requires a model item of type 'application.Models.TABLE1'.

My view:

@model application.Models.TABLE1

@{
    ViewBag.Title = "Update";
 }

<h2>Update</h2>

My controller logic for the view:

//GET: /TABLE1/Update/

    public ActionResult Update(string id)
    {
        IEnumerable<TABLE2> table2 = db.TABLE2;
        IEnumerable<TABLE1> table1 = db.TABLE1;
        string context = "Data Source=******;Initial Catalog=*******;User ID=******;Password=*****";

        foreach (var row in table1)
        {

            string strAddr = row.ADDRESS1 + "+" + row.CITY + "+" + row.ST + "+" + row.ZIP + "+" + row.COUNTRY;

            GoogleMapsDll.GeoResponse myCoordenates = new GoogleMapsDll.GeoResponse();
            myCoordenates = GoogleMapsDll.GoogleMaps.GetGeoCodedResults(strAddr);
            if (myCoordenates.Results != null && myCoordenates.Results.Length > 3)
            {
                string strLat = myCoordenates.Results[3].Geometry.Location.Lat.ToString();
                string strLong = myCoordenates.Results[3].Geometry.Location.Lng.ToString();

                using (SqlConnection myConnection = new SqlConnection(context))
                {
                    myConnection.Open();
                    string strQueryUpdate = "UPDATE TABLE1 SET Lat = '" + strLat + "' , Lng = '" + strLong + "'" + "WHERE ID = '" + row.ID + "' ";

                    SqlCommand myCommandUpdate = new SqlCommand(strQueryUpdate, myConnection);
                    myCommandUpdate.ExecuteNonQuery();

                }
            }

        }

        return View(table2);

    }

I have searched stack overflow, and found many answers regarding similar errors, but i cannot seem to find one that fits mine. I am not sure where or why a different model item is passed into the dictionary. Any suggestions or answers as to were my problem may be occurring or how it may be fixed would be helpful. If any more information would be helpful just ask and I will include it.

3

There are 3 answers

0
dav_i On BEST ANSWER

You need to change your model type in the view to match what you are passing as the model.

In your Controller, you have

IEnumerable<TABLE2> table2 = db.TABLE2;
// ...
return View(table2);

but in your view you have

@model application.Models.TABLE1

You either need to pass a single TABLE1 as your model into View(..) or change your view's model to be

@model IEnumerable<application.Models.TABLE2>

(The Dictionary the error is referring to is the ViewData dictionary)

0
DavidG On

Your view is expecting an instance of TABLE1 as the model but you are passing it an IEnumerable<TABLE2>. So not only do you have a mismatch of types, you are giving it the wrong entity type.

Either change your view to accept an IEnumerable, for example:

@model IEnumerable<application.Models.TABLE1>

Or only pass it a single instance, for example:

return View(table1.First());
0
Rik On

The error tells you exactly what is wrong.

Your view expects an object of type

@model application.Models.TABLE1

In your action method, you pass table2 to your view, which has the compile time type of IEnumerable<TABLE2> and the runtime type System.Data.Entity.DbSet<application.Models.model>

These types are different, so the View doesn't know what to do with it.