views:

159

answers:

1

I have a user control that will be used in several pages (as is the custom with user controls) that contains a form. I was thinking on how to guarantee its functionality.

First I wanted to have a single controller with a single method accept the values for this form. The only thing blocking me there is that after this method is done with the calculations etc it has to forward to an other controller and method depending on the view the user control originated on.

Then I wanted all the controllers that have this usercontrol to have the same fixed method that would forward then to the default page for that controller. This would only work if the fixed method had exactly one option to forward to (this is the case now, but maybe not tomorow) AND that I could somehow get the calling controller when I build my form tag.

Is there any clean way to either get one of my suggestions working or is there maybe an other option to get this thing going?

+2  A: 

You know, you can add public properties to your user control and then, in the markup, refer to those properties to do the appropriate things in the current context...

For example:

MyControl.ascx.cs:

/*snip*/
public string FormAction {get;set;}
/*snip*/

MyControl.ascx:

<% using(BeginForm(FormAction)){ %>
<!-- yadda -->
<% } %>

And in any aspx:

<!-- boring html goes here -->
<%=Html.RenderUserControl(“~/Views/Shared/MyControl.ascx”,null /*orwhatever*/, new {FormAction="ActionThatHandlesMyFormPostLol"})%>

BeginForm takes a number of different arguments that will allow you to control where and how your form posts. I'm not 100% clear on your issue, but I hope this gives you an idea on how to move forward on it....

Will
yes, this is something I was not aware of. I will have a check and give some feedback afterwards :)
borisCallens
Where exactly did you get the Html.RenderUserControl() method from?I render all my partials with RenderPartial() and it takes a ViewDataDictionary as the parameter you are suggesting
borisCallens
I'm using the mvc beta1 what version are you on?
borisCallens
I believe RenderUserControl is in Microsoft.Web.Mvc instead of System.Web.Mvc. If you download the source code and compile it then Microsoft.Web.Mvc will be in the output bin directory. I couldn't figoure out where else to get it from.
Todd Smith