1

I am having trouble deciding whether it is ok to construct HTML in controller actions and provide this HTML back to AJAX calls from my view (using jquery).

For instance, when selecting a client via jQuery Autocomplete, besides just getting the selected client ID we also need to construct a display or edit form for it. We might:

  1. Have like dozens of placeholder divs with proper IDs and receive Client JSON object from out controller action and then update those divs with the content from our object (very error prone, lots of IFs in our JS code, etc.),
  2. or instead of requesting for a Client JSON object rather request the prepared HTML and just insert it into the view (more appealing solution, logic is moved into controller and easiear to maintain - I rather maintain C# code than JS).

Do you think these are valid options? What do most modern apps do?

  1. While 2. will work perfectly for client's display form will it work for edit for?. Eedit form should contain HTML input controls because I want client properties to be POSTed back because when they are posted back to the controller I can materialize a viewmodel with them.
mare
  • 13,033
  • 24
  • 102
  • 191
  • here's an app that does render HTML in the controller's action (sometimes) http://mrgsp.md:8080/awesome/person – Omu Jan 31 '11 at 21:58

3 Answers3

2

Controller is almost never meant to do that.

Solutions are:

  • Use a particular view (be it partial) to achieve this
  • Send back JSON and construct tags on the client side using jquery/JavaScript
  • Create a custom HTML Helper to spit out necessary tags and script.

I would personally choose 3 and then 2.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Custom HTML helpers are pretty static in MVC, I mean I use them a lot but never within JS code - I can't call a helper based on users interactions with the page. I can only call controller actions (be it ones that return JSON or create one that returns HTML string). That's your option 2 and that's what I do now. – mare Jan 31 '11 at 13:30
  • 1
    I use HTML helper to **wire up** events and action by spitting out small javascript that calls functions I have in my static js files. Perhaps it is time you use it! – Aliostad Jan 31 '11 at 13:34
  • would you then focus on this third option and update your answer with some code? – mare Jan 31 '11 at 13:46
  • @Omu - you are right. I will update it and change to **almost never** :) – Aliostad Jan 31 '11 at 21:59
0

Can you not just use an Ajax form for this?

i.e

1: submit the form via jquery.

2: find and return a partial view based on whatever you're passing in as a parameter.

3: update the relevant div.

Lee Smith
  • 6,339
  • 6
  • 27
  • 34
  • I don't completely understand why should I need to submit the form either by ajax or by regular POST. I need to prepare the HTML based on the autocomplete textbox selection. – mare Jan 31 '11 at 14:07
  • Then just return partial views instead of JSON from the controller. The important thing is to keep the HTML out of the controller. – Lee Smith Jan 31 '11 at 14:16
0

Your controller should pass data to your view, not html.

I personally would make use of PartialViews, and jQuery Load() functionality to load those partial views based on the data supplied.

Community
  • 1
  • 1
stoic
  • 4,700
  • 13
  • 58
  • 88
  • I obviously missed that question, which is unfortunate since it deals both with render via controller action and via Load(). Thanks for pointing that out. – mare Feb 02 '11 at 01:16