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
@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
@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
@model ModelItemType.Models.A
@Html.DisplayFor(model => model.MyProperty)
Views/Shared/DisplayTemplates/AItem.cshtml Listing
@model ModelItemType.Models.A
<tr>
<td>@Html.DisplayFor(model => model.MyProperty)</td>
</tr>
Controllers/ActionController.cs Listing
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
{
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
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
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.DisplayFor(model => model.ACollection, "AItem")
Although if we change the line to:
@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.DisplayFor(model => model.ACollection, "AItem")
to:
@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ł