简体   繁体   中英

Generic object controller in MVC, can you improve my code?

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");

Have a look at ModelVisualizer in MVCContrib. It can display a collection of objects. You could start from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM