替换 Asp.net MVC Razor 中的 TextAreaFor 代码

发布于 2024-12-24 17:54:42 字数 728 浏览 1 评论 0原文

以下是我的模型属性

[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.
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2024-12-31 17:54:42

在渲染此视图的控制器操作中,确保您已实例化依赖属性(在您的情况下为 Product),以便它不为空:

非工作示例:

public ActionResult Foo()
{
    var model = new MyViewModel();
    return View(model);
}

工作示例:

public ActionResult Foo()
{
    var model = new MyViewModel
    {
        Product = new ProductViewModel()
    };
    return View(model);
}

另一种可能性(以及我的情况)推荐)是用 [DataType] 属性装饰您的视图模型属性,指示您将其显示为多行文本的意图:

[Required(ErrorMessage = "Please Enter Short Desciption")]
[StringLength(200)]
[DataType(DataType.MultilineText)]
public string ShortDescription { get; set; }

在视图中使用 EditorFor 帮助器:

@Html.EditorFor(x => x.Product.ShortDescription)

并 直到和您在问题中表达的担忧的 cols 参数,您可以简单地使用 CSS 来设置文本区域的宽度和高度。例如,您可以将此文本区域放入 div 或具有给定类名的内容中:

<div class="shortdesc">
    @Html.EditorFor(x => x.Product.ShortDescription)
    @Html.ValidationMessageFor(x => x.Product.ShortDescription)
</div>

并在 CSS 文件中定义其尺寸:

.shortdesc textarea {
    width: 150px;
    height: 350px;
}

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:

public ActionResult Foo()
{
    var model = new MyViewModel();
    return View(model);
}

Working example:

public ActionResult Foo()
{
    var model = new MyViewModel
    {
        Product = new ProductViewModel()
    };
    return View(model);
}

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:

[Required(ErrorMessage = "Please Enter Short Desciption")]
[StringLength(200)]
[DataType(DataType.MultilineText)]
public string ShortDescription { get; set; }

and in the view use an EditorFor helper:

@Html.EditorFor(x => x.Product.ShortDescription)

As far as the rows and cols 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:

<div class="shortdesc">
    @Html.EditorFor(x => x.Product.ShortDescription)
    @Html.ValidationMessageFor(x => x.Product.ShortDescription)
</div>

and in your CSS file define its dimensions:

.shortdesc textarea {
    width: 150px;
    height: 350px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文