从 ASP.NET MVC 2 中的文本框动态填充列表框
这就是我想要做的:1 个带有添加按钮的文本框,下面有一个带有删除按钮的列表框,以及一堆其他字段。您可能会猜到其功能:在文本框中键入一些内容并单击“添加”,它将将该字符串添加到下面的列表框中。在列表框中选择一个项目并单击“删除”,该项目将从列表中删除。我最终希望文本框也成为一个下拉列表。
让事情变得复杂的是,我动态生成 asp 页面,这意味着我无法将 c# 代码插入到 asp 页面中(不确定正确的名称是什么,但 <%= %> 标签在渲染中显示为文本)我还没有测试过,但这可能意味着 javascript 也不起作用)。
理想情况下,我希望将其全部保留为 ASP MVC 原生(但如果没有其他方法......)。我可以想出一些方法来做到这一点,但会留下一些令人讨厌的代码。最好的方法是什么?
Here's what i'm trying to do: 1 textbox with an Add button, and a ListBox underneath with a Delete button, along with a bunch of other fields. You can probably guess the functionality: type something in the text box and click Add, it will add that string into the listbox below. Select an item in the listbox and hit Delete, the item is removed from the list. I'd eventually like the textbox to be a dropdownlist as well.
Complicating things, i'm dynamically generating asp pages, which means i can't insert c# code into the asp page (not sure the what the proper name is, but <%= %> tags show up as text in the rendered page. I havent tested it yet, but it may mean javascript wont work either).
Ideally I'd like to keep it all native to ASP MVC (but if there's not other way...). I can think of some ways to do this but will leave behind some nasty code. What's the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法如下:当您点击文本框旁边的“添加”按钮时,您将收到回发或说您必须在控制器中点击“添加”操作。在“添加”操作中,您捕获在文本框中输入的文本,并将其添加到用于填充下拉列表的某个集合中。您必须在 Cache/Session/TempData 或它可以在页面周期之间生存的位置维护此集合。华泰
Here is the approach: When you hit Add button next to textbox, you will have a postback or say you will have to hit Add action in controller. In your Add action you capture the text that was entered in text box and add it to some collection using which you are populating your dropdown list. You will have to maintain this collection in Cache/Session/TempData or somewhere where it can survive between page cycles. HTH