1

So I have a View that contains 2 models. Each model has its own form and submit button.

Currently, I have both submit buttons are processed by the same controller method and use reflection to figure out which model type was passed. But it seems like there would be a better way... any ideas?

I have something like this:

Models:

public class Model1
{
  // Elements
}

public class Model2
{
  // Elements
}

Controller:

public ViewResult ConMeth(Object model)
{
  Type t = model.GetType();
  if(t == typeof(Model1)
  {
    // Do work for Model1
  }
  else if(t == typeof(Model2)
  {
    // Do work for Model2
  }
  else
  {
    // Do something else...
  }
}
11
  • What's the impetus for wanting to go to the same controller action... Posting to seperate actions would eliminate this problem seemingly Commented Apr 27, 2013 at 4:09
  • Do you want an example using ajax or regular postbacks? Commented Apr 27, 2013 at 4:09
  • @Rikon: Well that was the path I planned on. But I kept getting: The current request for action 'Manage' on controller type 'ConMeth' is ambiguous between the following action methods Commented Apr 27, 2013 at 4:12
  • @FeistyMango: Postback would be idea Commented Apr 27, 2013 at 4:13
  • @AlexeiLevenkov: I always consider (and perhaps in error), that looking at the metadata (.GetType()) to be a form of reflection. Methods just don't feel the same :) Commented Apr 27, 2013 at 4:14

2 Answers 2

1

If you show your view info, I suspect you've got two seperate things happening in the view. Just put each thing in it's own form and use the

@using (Html.BeginForm(...)){}

and specify the actions by name and the controller (if necessary) in the BeginForm params... That should get rid of the ambiguous reference error

Here is an example w/ the older (not razor) tags

Sign up to request clarification or add additional context in comments.

1 Comment

doh! Obviously... this fixed it.
0

You can use a Tuple<> in your view to have two view models, and then in the @Html.BeginForm() helper method for each form, you can specify POSTs to two different controllers to process your form data.

@model Tuple<ProjectName.Models.Model1, ProjectName.Models.Model2>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.