1

I have the following controller action:

public ActionResult SaveEdits(MagHeader viewModel) {
        AppRepository.SaveData(viewModel);
        return PartialView("DisplayForModel", viewModel);
    }

The ViewModel is very simple and just holds a few properties.

public class MagHeader {
    public string MagDate { get; set; }

    public string MagTitle { get; set; }

}

This all works just fine. However, the issue is that I have about 40 of these different viewmodels and a controller action for each. All it's doing is saving the data properties of each which I can do through reflection.

Is there a way to have one controller handle all of them? They are all string properties so really they are just key/value pairs. Is there some way to make the controller action generic?

Greg Gum
  • 33,478
  • 39
  • 162
  • 233
  • see if this helps...http://stackoverflow.com/questions/20009031/how-to-use-asp-net-mvc-generic-controller-to-populate-right-model – foxtrotZulu Mar 24 '14 at 20:48
  • Well, I read over the link as well as quite a few other ones, and I don't see a way of doing what I wanted which was to type the controller actions at runtime based off a string. – Greg Gum Mar 24 '14 at 21:59
  • IMHO, best option is to create a binder (IModelBinder) and instead or receiving a MagHeader or other, receive a Dictionary. – Anderson Matos Mar 27 '14 at 02:11
  • Sounds like a good idea. If you want to post this as answer (with maybe a brief example) I will accept it. – Greg Gum Mar 27 '14 at 15:23

0 Answers0