如何在MVC2视图中包含UserControl和Model?

发布于 2024-10-23 21:04:15 字数 164 浏览 2 评论 0原文

我有用于特定视图的用户控件和模型。用户控件工作正常,同时我尝试将单选按钮包含在与模型相同的视图上。我收到错误:“传递到字典中的模型项的类型为“System.Data.DataTable”,但该字典需要类型为“MyModelName”的模型项”。

那么你能帮助任何人吗?

谢谢, 莫罕

I have UserControl as well as Model for particular view. Usercontrol is working properly 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-10-30 21:04:15

异常消息的描述性很强,说明了一切。您的视图接受不同的模型,并且您将不同的模型传递给控制器​​中的该视图。
看看两个地方

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

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

,在你的控制器中,你会有类似

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

Edit 的东西,你可以使用 viewModel ,它可以包含视图所需的所有值,

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

现在在控制器中,你可以像这样填充你的 viewModel

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

,并且在你应该更新的视图中它接受 MyViewModel 类型

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

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

,然后您可以使用在视图中访问它们

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

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%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文