简体   繁体   English

ASP.NET MVC3清除表格

[英]ASP.NET MVC3 Clear Form

I'm new to asp.net mvc and I couldn't find the best way to do this: 我是asp.net mvc的新手,我找不到最佳的方法:

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); ViewBag.DDLCONTENT = .... (并从数据库中获取);

If I repopulate the form in the controller with ajax, ViewBag.DDLCONTENT will become empty. 如果我使用ajax重新填充控制器中的表单,则ViewBag.DDLCONTENT将为空。 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. DropDownList仅将选定的值发送到服务器。 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. 将下拉列表放入div,然后在重新填充表格之前克隆div。 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. 无论哪种方式,由于都是通过AJAX客户端完成的,因此您的解决方案将需要来自客户端代码,例如javascript或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. Darin确实很好地说明了新数据。 If the data could possibly be outdated then the database should be called another time. 如果数据可能已过时,则应再次调用该数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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