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...
  }
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
poy
  • 10,063
  • 9
  • 49
  • 74
  • What's the impetus for wanting to go to the same controller action... Posting to seperate actions would eliminate this problem seemingly – Rikon Apr 27 '13 at 04:09
  • Do you want an example using ajax or regular postbacks? – Matthew Cox Apr 27 '13 at 04: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` – poy Apr 27 '13 at 04:12
  • @FeistyMango: Postback would be idea – poy Apr 27 '13 at 04: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 :) – poy Apr 27 '13 at 04:14
  • (Not to be an ass, but it's just a post... not a postback) – Rikon Apr 27 '13 at 04:20
  • Hmmm need more coffee and closing braces... Yes, GetType() is reflection, but as I see no reason why you are using it there - so did not see the call in the code :)... – Alexei Levenkov Apr 27 '13 at 04:21
  • @Rikon `... not to be an ass ...` Kinda ended up being one. http://en.wikipedia.org/wiki/Postback – Matthew Cox Apr 27 '13 at 05:32
  • @FeistyMango... It's not the same thing... MVC doesn't use PostBacks... That's a web form thing (necessary for the page lifecycle)... MVC uses pure posts... If you decide to manipulate the model and return it to the same view, that's your business, but it's not the baked-in, strongly embraced concept of web-forms... If we're quoting other web pages: http://stackoverflow.com/questions/3505649/asp-net-mvc-postback – Rikon Apr 27 '13 at 05:42
  • @Rikon Postback isn't a term specifically owned by ASP.NET my friend. It's a term used in a variety of web development languages. http://stackoverflow.com/questions/183254/what-is-a-postback – Matthew Cox Apr 27 '13 at 05:55
  • I totally get where you're coming from, and I guess we'll have to agree to disagree, but in the generic MVC pattern (which is what I'm assuming due to the question tags), the controller decides what to do and not just a defacto post back... Nor does MVC care about from whence it last came unlike (again, I'm using) webforms, in which knowing IsPostBack is critical in determining lifecycle logic. I love these kinds of arguments... I'd love to pick knits with you if you want to keep going (it's late, so this is my last for the night) – Rikon Apr 27 '13 at 06:27

2 Answers2

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

Rikon
  • 2,688
  • 3
  • 22
  • 32
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>
A Biz
  • 37
  • 1
  • 9