开发者

How to include UserControl and Model in MVC2 view?

I have UserControl as well as Model for particular view. Usercontrol is working prope开发者_开发问答rly at the same time i try to include radiobutton on the same view with model. I am getting the Error:"The model item passed into the dictionary is of type 'System.Data.DataTable', but this dictionary requires a model item of type "MyModelName"".

So can you please help anyone.

thanks, Mohan


The Exception Message is very descriptive and says it all. your view accepts a different model and you are passing different model to this view in controller. Look at two places

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 

Inherits="System.Web.Mvc.ViewPage<AcceptedModel>" %>

and in your controller you would have something like

public ActionResult action()
{
    SentModel model = new SentModel(); 
    return View(SentModel); //i believe typeof(SentModel) != typeof(AcceptedModel) that is what is causing problem
}

Edit you can use viewModel that can contain all values required by the view

public class MYViewModel
{
    System.Data.DataTable MyTable{get;set;}
    Registration Myregistration{get;set;}
}

now in controller you can populate your viewModel like

public ActionResult MyActionResult(int id)
{
   MyViewModel mdl = new MyViewModel();
   mdl.Myregistration = new Registration();
   mdl.MyTable = //code to populate table
   return View(mdl);
}

and in the view you should update it to accept MyViewModel type

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 

Inherits="System.Web.Mvc.ViewPage<MyViewModel>" %>

and then you can access them in view using

<%foreach( var row in Model.MyTable){}%>
and <%:Model.MyRegistration.FirstName%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜