2

I'm new to asp.net mvc and I couldn't find the best way to do this:

I have a form with a dropdown list. In the controller, on populating the form, I set:
ViewBag.DDLCONTENT = .... (and take it from the database);

If I repopulate the form in the controller with ajax, ViewBag.DDLCONTENT will become empty. So exactly should I do this without having to call again the database?

I can post the full code if my question is not clear enough
Thank you

3
  • If you could post a sample of what you have done so far that would definitely make it easier to help you. Commented Sep 20, 2012 at 8:46
  • if you call server ViewBag is re-set value. every time set value in ViewBag. Commented Sep 20, 2012 at 8:50
  • If the list of values are not large in number, you can use session object to store that list and use it to repopulate the DDLContent again in Ajax actions Commented Sep 20, 2012 at 9:09

2 Answers 2

1

So exactly should I do this without having to call again the database?

Call the database again. The DropDownList sends only the selected value to the server. Or if you want to avoid calling it you could store those values in the cache. But if the data in the database changes in between you probably want to call it anyway to retrieve fresh data.

Sign up to request clarification or add additional context in comments.

Comments

0

Place the dropdown in a div and then just clone the div before you repopulate the form. Either way, since this is being done with AJAX client side, your solution will need to come from client side code such as javascript or jquery.

<div id="ddlClone">@Html.DropDownFor()</div>
<script type="text/javascript">
 var cloneForLater = $("#ddlClone").clone(true);
</script>

Darin does make a good point about fresh data. If the data could possibly be outdated then the database should be called another time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.