MVC 中的通用对象控制器,你能改进我的代码吗?
我正在创建一个应用程序,它将在数据网格中显示对象列表(任何类型的对象的列表),并允许用户更新任何项目。在运行时之前,代码对正在显示的对象一无所知。您可以改进我的更新代码吗?我使用 Formcollection 从表单中获取项目,并根据路由信息创建类的实例,即它将从 URL 中提取对象(本例中为硬编码的 Employee)并创建它的实例。
[HttpPost]
public ActionResult Details(FormCollection Collection)
{
try
{
foreach (var item in Collection)
{
//TODO set up form values container for populating new object
string test = Collection[item.ToString()];
}
Assembly CurrentAssembly =
Assembly.GetExecutingAssembly();
dynamic updateObject = CurrentAssembly.CreateInstance("Employee");
I am creating an application that will display a list of objects in a datagrid (list of any type of object), and allow the user to update any item. The code will know nothing about the object being displayed until runtime. Can you improve my code for the update? I am using Formcollection to get items from the form and creating an instance of my class based on the Routing info, ie it will pull out the object (hardcoded Employee for this example) from URL and create an instance of it.
[HttpPost]
public ActionResult Details(FormCollection Collection)
{
try
{
foreach (var item in Collection)
{
//TODO set up form values container for populating new object
string test = Collection[item.ToString()];
}
Assembly CurrentAssembly =
Assembly.GetExecutingAssembly();
dynamic updateObject = CurrentAssembly.CreateInstance("Employee");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 MVCContrib 中的 ModelVisualizer。它可以显示对象的集合。你可以从那里开始。
Have a look at ModelVisualizer in MVCContrib. It can display a collection of objects. You could start from there.