asp.net mvc: What is the correct way to return html from controller to refresh select list?

Posted by Mark Redman on Stack Overflow See other posts from Stack Overflow or by Mark Redman
Published on 2010-03-30T10:07:57Z Indexed on 2010/03/30 10:53 UTC
Read the original article Hit count: 318

Filed under:
|

Hi,

I am new to ASP.NET MVC, particularly ajax operations. I have a form with a jquery dialog for adding items to a drop-down list. This posts to the controller action.

If nothing (ie void method) is returned from the Controller Action the page returns having updated the database, but obviously there no chnage to the form. What would be the best practice in updating the drop down list with the added id/value and selecting the item. I think my options are:

1) Construct and return the html manually that makes up the new <select> tag [this would be easy enough and work, but seems like I am missing something]

2) Use some kind of "helper" to construct the new html [This seems to make sense]

3) Only return the id/value and add this to the list and select the item [This seems like an overkill considering the item needs to be placed in the correct order etc]

4) Use some kind of Partial View [Does this mean creating additional forms within ascx controls? not sure how this would effect submitting the main form its on? Also unless this is reusable by passing in parameters(not sure how thats done) maybe 2 is the option?]

UPDATE:

Having looked around a bit, it seems that generating html withing the controller is not a good idea. I have seen other posts that render partialviews to strings which I guess is what I need and separates concerns (since the html bits are in the ascx). Any comments on whether that is good practice.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jquery-ajax