在Asp.net MVC Razor中替换TextAreaFor代码 [英] Replacement for TextAreaFor code in Asp.net MVC Razor

查看:68
本文介绍了在Asp.net MVC Razor中替换TextAreaFor代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的模型属性

[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.

现在,由于Microsoft MVC3版本中存在错误,我无法进行验证,然后提交表单并产生恼人的错误.

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.

请告诉我解决方法或可以替代TextAreaFor的任何代码.我不能使用EditorFor,因为有了它,就不能使用rows和cols参数.我想在浏览器中保持现场外观.让我知道在这种情况下应该怎么做

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

推荐答案

在呈现此视图的控制器操作中,请确保实例化了依赖属性(在您的情况下为Product),以使其不为空:

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:

非工作示例:

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

工作示例:

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

另一种可能性(也是我推荐的一种可能性)是用[DataType]属性修饰视图模型属性,以表明您打算将其显示为多行文本:

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; }

,然后在视图中使用EditorFor助手:

and in the view use an EditorFor helper:

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

对于您在问题中表示关注的rowscols参数,您可以简单地使用CSS设置文本区域的宽度和高度.例如,您可以将此文本区域放入div或具有给定类名的内容中

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>

,然后在CSS文件中定义其尺寸:

and in your CSS file define its dimensions:

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

这篇关于在Asp.net MVC Razor中替换TextAreaFor代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆