Http Form Posts - Data Access

Topics: Writing modules
Sep 24, 2012 at 8:34 PM

I have a custom control that the orchard author will place on a page that has some basic settings; then when the page is rendered; my "Driver" is called and the control renders with those basic settings within a form.

When the user changes an option - there is a post that is performed that then calls my "Controller" with parameters based on some of the form values.

How do I access the other values that are tied to the configuration of that control from within the "Controller" post handler?

If I follow some of the other examples for returning from this function - I see that a new view is created based on the current Shape.ViewModel() - is this really the correct proceadure?  it almost seems like it's duplicating my "Driver" display function - which builds a model and returns.

Also I've noticed that even if I return from this function - just hardcoding the values from the configuration since I don't yet know how to access the existing values (not the postback paramters; but the settings of the control as to how it was configured - (e.g. the parts) - I'll get a "View" not found exception because it's ignoring any alternates and is looking in the root of my module views for the view - which this particular view is located in "/Views/Parts".

Should I toss this and just go completely AJAX for doing the update? - I could; but am curious as to how I could make this work for now and then go back and convert to AJAX later and just return a Jason array on the form post.



Sep 24, 2012 at 10:14 PM

Well, I'm not sure what you mean by "control" but you need enough information to be passed along in the form submission for your controller to understand. The id of the content item should be sufficient.

I'm not following your question about returning from the action. Please explain. It looks like what you really want to do is redirect to the display url for your content item.

Sep 25, 2012 at 4:45 PM

I think you answered it - I should redirect instead of trying to return a shape?

Great idea - I didn't think to pass the id of the control instead of all the settings via the post (would be cleaner - but not sure of the perf hit; if only a few settings; better through the post)

I was trying to just return the shape - but because I was trying to return the shape - it looks like none of the views/templates were being used therefore it couldn't find any and gave an error looking for the template.  A redirect with all the same parameters and then let the "Driver" parse the parameters almost looks better; but I'm not sure it's the right design? What is the best practice in this situation?

I essentially want to handle events from a few checkboxes that modify the return results from the data.  a very high percentage of the time - this would be done via AJAX; but in this case I was doing it via server side.  Is it best practice to just redirect with parameters that will help it determine what to render; or is there a way to return the shape in such a way it will find the alternates (or do I have to manually add the alternates after creating the shape)

Lastly - I haven't seen any examples - but how do I return just Jason or XML from a HTTP post being handled from the "Controller"?  Right now it wraps the content with headers footers; which I assume is because of the "default" layer - which means I may have to modify the "default" layer to not include any of the data controller URL paths - correct?

Thanks - you always seem to have the answer!


Sep 25, 2012 at 4:49 PM

Redirecting is the standard way of doing things after a form post. Just return a redirect result from your action.

For JSON, there is a JSON result in MVC that you can use. It should not wrap your stuff. In general, to disable that kind of wrapping, you can also decorate your action with [Themed(false)]. Layers have pretty much nothing to do with it ;)