如何使用 ASP.NET MVC 在视图页面中显示自定义对象属性?
我正在尝试将 ASP.NET MVC 功能(尤其是路由)添加到我现有的 ASP.NET Web 应用程序中。我添加了一个控制器和视图(一个 asp.net 页面)。
现在我想知道如何在视图中显示自定义类(例如用户)的对象的详细信息?我可以分配 ViewData 集合中的对象并将其呈现在视图中吗?我已经有一个为当前 ASP.NET Web 应用程序运行的 Datalayer(在 ADO.NET 中),所以我想使用它。
我在我的控制器中尝试了这个
public ActionResult Index()
{
BusinessObject.User objUser = new BusinessObject.User();
objUser.EmailId = "[email protected]";
objUser.ProfileTitle = "Web developer with 6 yrs expereince";
ViewData["objUser"] = objUser;
ViewData["Message"] = "This is ASP.NET MVC!";
return View();
}
如何在视图页面中使用它来显示用户详细信息?
I am trying to add ASP.NET MVC features (especially routing) to my already existing ASP.NET web application. I added a Controller and View (an asp.net page).
Now I want to know how can I display the details of object of my custom class (for example, User) in the view? Can I assign the object in ViewData collection and render it in the view? I already have a Datalayer (in ADO.NET) which is running for the the current ASP.NET web app, so I want to use that.
I tried this in my controller
public ActionResult Index()
{
BusinessObject.User objUser = new BusinessObject.User();
objUser.EmailId = "[email protected]";
objUser.ProfileTitle = "Web developer with 6 yrs expereince";
ViewData["objUser"] = objUser;
ViewData["Message"] = "This is ASP.NET MVC!";
return View();
}
How can I use this in the view page to display the user details ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将对象作为视图模型(或视图模型的一部分,以及您的消息)传递给强类型视图。然后您可以简单地在视图中引用模型属性。
(或者,更好的是,只是您真正需要的用户对象的属性)
控制器
视图
You should pass your object as a view model (or part of a view model, along with your message) to a strongly-typed view. Then you can simply reference the model properties in your view.
(or, better yet, just the properties from the user object that you really need)
Controller
View