使用动态创建的控件进行 asp.net mvc 1 验证
使用 ASP.Net MVC1 并在模型中动态创建 html,然后将其放入视图中并在运行时呈现。
我的视图是一个如下所示的单个页面:
<%@ Page Language="VB" Inherits="System.Web.Mvc.ViewPage" %>
<%=(ViewData("Content"))%>
此动态创建的内容主要是格式为的下拉列表框
<form method=post action="/questions/nextBatch">
<div id="text">What is your preferred drink?</div>
<select>
<option selected value="noSelect"> - Please make a selection - </option>
<option value="a" >Milk</option>
<option value="b" >Coffee</option>
<option value="c" >Tea</option>
</select>
</form>
是否可以对下拉列表执行验证以确保已选择某个值?如果任何下拉列表仍然具有默认值(“请进行选择”文本),则页面会重新显示,并将问题或下拉列表框的字体颜色更改为红色?
我来自 Webforms 世界,已经切换到 MVC(我真的很喜欢!),我知道有一些非常灵活的验证工具,但您似乎必须在视图本身中考虑它们。
我无法通过控制器的视图数据传递 HTML 帮助程序,因为除非将其直接编码到视图本身中,否则它不会呈现。
另外,我正在寻找一个纯 asp.net 解决方案,因为由于可访问性问题我无法使用 javascript。我可以通过 formscollection 对象访问下拉列表的所有值,没有任何问题,但从那里我有点卡住了。
有什么我错过的吗?
提前非常感谢您的任何建议或帮助!
Using ASP.Net MVC1 and am dynamically creating the html in a model that is then dropped into the view and rendered at run time.
My view is a single page that looks like this:
<%@ Page Language="VB" Inherits="System.Web.Mvc.ViewPage" %>
<%=(ViewData("Content"))%>
This dynamically created content is mostly dropdownlist boxes in the format of
<form method=post action="/questions/nextBatch">
<div id="text">What is your preferred drink?</div>
<select>
<option selected value="noSelect"> - Please make a selection - </option>
<option value="a" >Milk</option>
<option value="b" >Coffee</option>
<option value="c" >Tea</option>
</select>
</form>
Is it is possible to perform validation on the dropdowns lists to ensure that a value has been selected? If any of the dropdownlists that still have the default value (the "please make a selection" text) the page is then redisplayed with the font colour of the question or dropdownlist box changed to say red?
I'm from the Webforms world and have made the switch over to MVC (which I really like!) and I know there are some pretty slick validation tools out there but you seem to have to account for them within the view itself.
I cannot pass a HTML helper through the viewdata of the controller as it does not render unless it has been coded directly into the view itself.
Also, I'm looking for a pure asp.net solution as I cannot use javascript due to accessiblity concerns. I can access all the values of the dropdowns without any problems throug the formscollection object but from there I'm a bit stuck.
Is there something out there that I'm missing?
Thank you so much in advance for any advice or help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您无法使用 Javascript,则唯一进行验证的方法是服务器端。
您可以使用内置的 ModelState 对象来处理错误。这篇 Scott Guthrie 博客文章“ASP.NET MVC 2:模型验证”解释了一切
If you can't use Javascript, the only way to do validation is server side.
You can use the built-in ModelState object to manipulate the error. This Scott Guthrie blog post "ASP.NET MVC 2: Model Validation" explains it all