MVC3 Razor Html.TextBoxFor:我可以使其像 TextAreaFor 一样多行吗?
我已经看过这篇文章:更改 Html.TextBox 的大小
但是我无法更改为 Html.TextAreaFor()
因为它没有让我设置动态默认值的属性(我正在制作一个编辑页面,我想动态填充当前的字段)数据可以然后进行修改)。即使我在视图模型中更改为 [DataType(DataType.MultilineText)]
(它似乎更改为 textboxfor 并且我的默认值确实如此),我似乎也无法让 EditorFor 工作不出现)。
我的编辑视图采用一个包含两个模型的视图模型、一个要填充屏幕字段的空表单 (OfferForm
) 以及包含当前数据的 offer
模型。我想要为 OfferForm
提供文本字段,并用 offer
模型数据的内容动态填充它们。
这是我的 html:
<div class="InLine">
<div class="InLine editor-label-container">
<div class="editor-label">
@Html.LabelFor(model => model.OfferForm.Description)
</div>
</div>
<div class="InLine editor-field-container">
<div class="editor-field">
@Html.TextBoxFor(model => model.OfferForm.Description, new { @Value = @Html.DisplayFor(model => model.Offer.Description)})
@Html.ValidationMessageFor(model => model.Offer.Description)
</div>
</div>
</div>
<div class="InLine">
<div class="InLine editor-label-container">
<div class="editor-label">
@Html.LabelFor(model => model.OfferForm.SMSText)
</div>
</div>
<div class="InLine editor-field-container">
<div class="editor-field">
@Html.TextBoxFor(model => model.OfferForm.SMSText, new { @Value = @Html.DisplayFor(model => model.Offer.SmsText)})
@Html.ValidationMessageFor(model => model.Offer.SmsText)
</div>
</div>
</div>
有没有办法在 Html.TextBoxFor()
中设置“multiline
”属性?
I've already seen this post: Changing the size of Html.TextBox
But I cannot change to Html.TextAreaFor()
because it does not have an attribute for me to set a dynamic default value (I am making an edit page that I want to dynamically fill in the fields of the current data which can then be modified). I also can't seem to get the EditorFor to work even if I change to [DataType(DataType.MultilineText)]
in my viewmodel (all it does it seem to change to textboxfor and my default values do not show up).
My edit view takes in a viewmodel containing two models, an empty form (OfferForm
) to be filled with the onscreen fields, and the offer
model containing the current data. I want to have textfields for the OfferForm
, and dynamically fill them with the contents of the offer
model's data.
Here is my html:
<div class="InLine">
<div class="InLine editor-label-container">
<div class="editor-label">
@Html.LabelFor(model => model.OfferForm.Description)
</div>
</div>
<div class="InLine editor-field-container">
<div class="editor-field">
@Html.TextBoxFor(model => model.OfferForm.Description, new { @Value = @Html.DisplayFor(model => model.Offer.Description)})
@Html.ValidationMessageFor(model => model.Offer.Description)
</div>
</div>
</div>
<div class="InLine">
<div class="InLine editor-label-container">
<div class="editor-label">
@Html.LabelFor(model => model.OfferForm.SMSText)
</div>
</div>
<div class="InLine editor-field-container">
<div class="editor-field">
@Html.TextBoxFor(model => model.OfferForm.SMSText, new { @Value = @Html.DisplayFor(model => model.Offer.SmsText)})
@Html.ValidationMessageFor(model => model.Offer.SmsText)
</div>
</div>
</div>
Is there a way to set a "multiline
" attribute in the Html.TextBoxFor()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须意识到 HTML 元素不是 Windows 控件,它们只是在某种程度上映射到它们。
HTML 元素必须是跨平台的,并且不能保证文本区域只是所有平台上的多行文本框,因此就 HTML 而言,文本框和文本区域是两个不同的东西。
所以答案是否定的,因为 HTML 中的文本框没有多行属性。
You have to realize that HTML elements are not windows controls, they just map to them somewhat.
HTML elements have to be cross platform, and there is no guarantee that a textarea is just a multi-line textbox on all platforms, so as far as HTML is concerned a textbox and a textarea are two different things.
So the answer is no, because there is no multiline attribute for a textbox in HTML.
最简单的方法是创建一个如下所示的 Html 扩展方法,
然后您可以调用它:
它只支持基本功能和不支持的功能。实际的标记生成是从 Mvc3 源中提取的,并进行了一些细微的更改以支持传入值。这应该适用于大多数情况,但您可能需要添加更多覆盖。这不是很干净,但应该可以让你继续。 :)
要让扩展显示在您的 razor 视图中,您可以在 Views 目录中编辑 Web.config 并添加命名空间(不是类名)放置该类的位置。然后它应该显示在智能感知中。
The simplest way to proceed would be to create an Html extension method like the following
You could then call it:
It only supports basic features and what not. The actual tag generation was pulled from Mvc3 source with slight changes to support passing the value in. This should work for most cases but you might need to add more overrides. This isn't very...clean but should get you going. :)
To get the extension to show up inside your razor views you edit the
Web.config
within the Views directory and add the namespace (not the class name) where you put this class. It should then show up in intellisense.