替换 Asp.net MVC Razor 中的 TextAreaFor 代码
以下是我的模型属性
[Required(ErrorMessage = "Please Enter Short Desciption")]
[StringLength(200)]
public string ShortDescription { get; set; }
以下是我相应的视图代码
@Html.TextAreaFor(model => model.Product.ShortDescription, new { cols = "50%", rows = "3" })
@Html.ValidationMessageFor(model => model.Product.ShortDescription)
这就是它在浏览器中显示的方式,这是我想要的方式。
现在,由于有一个 Microsoft MVC3 版本中的错误,我无法验证表单并提交并产生烦人的错误。
请告诉我解决方法或任何可以代替 TextAreaFor 的代码。我无法使用 EditorFor,因为使用它,我无法使用 rows 和 cols 参数。我想在浏览器中保持我的字段外观。让我知道在这种情况下应该做什么
Following is my model property
[Required(ErrorMessage = "Please Enter Short Desciption")]
[StringLength(200)]
public string ShortDescription { get; set; }
And following is my corresponding View code
@Html.TextAreaFor(model => model.Product.ShortDescription, new { cols = "50%", rows = "3" })
@Html.ValidationMessageFor(model => model.Product.ShortDescription)
And this is how it shows in the browser, the way i want.
Now, since there is a bug in Microsoft's MVC3 release, I am not able to validate and the form is submitted and produces the annoying error.
Please tell me the work around or any code that can be substituted in place of TextAreaFor. I can't use EditorFor, because with it, i can't use rows and cols parameter. I want to maintain my field look in the browser. Let me know what should be done in this case
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在渲染此视图的控制器操作中,确保您已实例化依赖属性(在您的情况下为
Product
),以便它不为空:非工作示例:
工作示例:
另一种可能性(以及我的情况)推荐)是用
[DataType]
属性装饰您的视图模型属性,指示您将其显示为多行文本的意图:在视图中使用
EditorFor
帮助器:并 直到
行
和您在问题中表达的担忧的cols
参数,您可以简单地使用 CSS 来设置文本区域的宽度和高度。例如,您可以将此文本区域放入 div 或具有给定类名的内容中:并在 CSS 文件中定义其尺寸:
In the controller action rendering this view make sure you have instantiated the dependent property (
Product
in your case) so that it is not null:Non-working example:
Working example:
Another possibility (and the one I recommend) is to decorate your view model property with the
[DataType]
attribute indicating your intent to display it as a multiline text:and in the view use an
EditorFor
helper:As far as the
rows
andcols
parameters that you expressed concerns in your question about, you could simply use CSS to set the width and height of the textarea. You could for example put this textarea in a div or something with a given classname:and in your CSS file define its dimensions: