简体   繁体   中英

ASP.NET MVC3 Clear Form

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM