Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to solve this type of problem?
Posted
Comments
hitech_s 25-May-12 7:10am    
are you using entity datamodel?

First of all, the title is for short title related to issue asked and not for full error or the question itself!

Now, clearly the error specifies that there is a datatype mis-match. The model item that you pass is not of type Demo.Models.CustomerData as expected. Instead you are passing a list of Demo.Models.CustomerData.

Resolve the datatype issue.
 
Share this answer
 
Comments
sajan064 23-May-12 5:48am    
cant we show the list of data from database and fill up form to the same page like
1st view
@model Demo.Models.CustomerData
@html.editorfor(m=>m.fname)

2nd view
@model ienurable<demo.models.customerdata>

<table>
<tr>
<th>
FName

</th>
</tr>
@foreach (var item in model)
{
@html.DisplayFor(modelitem=>item.FName)

}
</table>


how to show this two views at same view using partial view
Hi Sajan,

I have faced today a similar problem. The Exception was like yours but for a diffrent reasons. In a single View I wanted to place three different controls. All of them strongly typed to class A. First was editor control, second was displaying read only and looked similar to a form and third represented item in a collection of records. These are the files I have created:

Controllers/ActionController.cs
Models/A.cs
Models/ActionViewModel.cs
Views/Shared/DisplayTemplates/A.cshtml - form like display
Views/Shared/DisplayTemplates/AItem.cshtml - item for a table display
Views/Shared/EditorTemplates/A.cshtml - ediatble form
Views/Action/Action.cshtml - main view for the page

Views/Action/Action.cshtml Listing
HTML
@model ModelItemType.Models.ActionViewModel

@{
    ViewBag.Title = "Action";
}

<h2>Action</h2>
@if (Model.AInstance.IsEditable)
{
@Html.EditorFor(model => model.AInstance)
}
else
{
@Html.DisplayFor(model => model.AInstance)
}
@if (Model.ACollection.Count > 0)
{
<table>
@Html.DisplayFor(model => model.ACollection, "AItem")
</table>
}


Views/Shared/EditorTemplates/A.cshtml Listing
HTML
@model ModelItemType.Models.A
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        @Html.EditorFor(model => model.MyProperty)
        @Html.ValidationMessageFor(model => model.MyProperty)
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}


Views/Shared/DisplayTemplates/A.cshtml Listing
HTML
@model ModelItemType.Models.A

@Html.DisplayFor(model => model.MyProperty)


Views/Shared/DisplayTemplates/AItem.cshtml Listing
HTML
@model ModelItemType.Models.A

<tr>
    <td>@Html.DisplayFor(model => model.MyProperty)</td>
</tr>


Controllers/ActionController.cs Listing
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ModelItemType.Models;

namespace ModelItemType.Controllers
{
    public class ActionController : Controller
    {
        //
        // GET: /Action/

        public ActionResult Action()
        {
            ActionViewModel viewModel = new ActionViewModel
            {
                AInstance = new A { MyProperty = "PropertySet1", IsEditable = true },
                ACollection = new List<a> {
                    new A{ MyProperty = "PropertySet2", IsEditable = true},
                    new A{ MyProperty = "PropertySet3", IsEditable = false}
                }
            };
            return View(viewModel);
        }

    }
}


Models/A.cs Listing
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ModelItemType.Models
{
    public class A
    {
        public string MyProperty { get; set; }
        public bool IsEditable { get; set; }
    }
}


Models/ActionViewModel.cs Listing
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ModelItemType.Models
{
    public class ActionViewModel
    {
        public A AInstance { get; set; }
        public ICollection<a> ACollection { get; set; }
    }
}


This code throws the similar exception as in the title of this post. Look at the Views/Action/Action.cshtml:
HTML
@Html.DisplayFor(model => model.ACollection, "AItem")

Although if we change the line to:
HTML
@Html.DisplayFor(model => model.ACollection)

The model class is the same. But I was not able to have two controls bound: one to the single instance one to the collection.

Your edited question was:
"cant we show the list of data from database and fill up form to the same page like 1st view"
Solution is:
Yes we can! Just do the following to the views:
1. Change the following line in Views/Action/Action.cshtml:
HTML
@Html.DisplayFor(model => model.ACollection, "AItem")

to:
HTML
@Html.DisplayFor(model => model.ACollection)

2. Remove Views/Shared/DisplayTemplates/A.cshtml
3. Rename Views/Shared/DisplayTemplates/AItem.cshtml to Views/Shared/DisplayTemplates/A.cshtml

I think this solves your problem.

Unfortunately I'm left with mine. Why my first version does not want to work?

Kind Regards,
Paweł
 
Share this answer
 
v2
Comments
arehman_2001 28-Jun-12 0:45am    
Hi Paweł
I am facing the same problem, but could not resolve it, did you get any solution? waiting for your kind response...

My questioin will be same as that of sajan064

cant we show the list of data from database and fill up form to the same page


Regards
Abdul rehman

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900