asp.net-mvc asp.net mvc 中的 Html.TextAreaFor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5893171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:10:28  来源:igfitidea点击:

Html.TextAreaFor in asp.net mvc

asp.net-mvctextareahtml-helper

提问by Ajai

I have a asp.net mvc view that allows the user to enter some description in a textarea.

我有一个 asp.net mvc 视图,允许用户在 textarea 中输入一些描述。

There are two problems that I am facing.

我面临两个问题。

Either when I expand the textarea it is expanding without moving other html elements or I am not able to make create a Html.TextBoxFor () multiline textbox. Can anyone suggest a solution to this? If Use Textarea how to make it expand(grow in size) so that it does not overlap with other elements or how to use Html.TextBoxFor() for multiline?

当我扩展 textarea 时,它会在不移动其他 html 元素的情况下扩展,或者我无法创建 Html.TextBoxFor () 多行文本框。任何人都可以提出解决方案吗?如果使用 Textarea 如何使其扩展(增大大小),使其不与其他元素重叠或如何使用 Html.TextBoxFor() 进行多行?

This is how my code looks like

这是我的代码的样子

<% using (Html.BeginForm())
        { %>
     <%: Html.ValidationSummary(true)%>

    <fieldset>

        <div class="editor-label1">
            <%: Html.LabelFor(Model => Model.PackageID)%>
        </div>
        <div class ="editor-field1">
             <%: Html.HiddenFor(Model => Model.PackageID)%>
              <%: Html.DisplayFor(Model => Model.PackageID)%>
             <%: Html.ValidationMessageFor(Model => Model.PackageID)%>
        </div>

         <div class="editor-label1">
            <%: Html.LabelFor(Model => Model.PackageName)%>
        </div>
        <div class ="editor-field1">
            <%: Html.TextBoxFor(Model => Model.PackageName)%>
            <%: Html.ValidationMessageFor(Model => Model.PackageName)%>
        </div>

        <div class="editor-label1">
            <%: Html.LabelFor(Model => Model.PackageDesc)%>
        </div>
        <div class ="editor-field1" style= "padding-bottom: 50px; margin-bottom: 150px">
            <%: Html.TextBoxFor(Model => Model.PackageDesc, new { TextMode = TextBoxMode.MultiLine, cols = "55", rows = "10" })%>

            <%: Html.ValidationMessageFor(Model => Model.PackageDesc)%>
        </div>

         <div class="editor-label1">
            <%: Html.LabelFor(Model => Model.PackageTitle)%>
        </div>
        <div class ="editor-field1">
            <%: Html.TextBoxFor(Model => Model.PackageTitle)%>
            <%: Html.ValidationMessageFor(Model => Model.PackageTitle)%>
        </div>
        <div class ="editor-label">
            <%: Html.Label("Project ID") %>
        </div>
        <div class="editor-field">
            <%:Html.DropDownList("ProjectID", (IEnumerable<SelectListItem>)ViewData["projects"])%>
        </div>
         <div>
                <input type="submit" value="Save Edit" />
         </div>
    </fieldset>
    <% } %>

回答by Muhammad Adeel Zahid

Rendering TextAreausing ASP.NET MVC's Htmlhelper and making it resizable are two different concerns. When using Htmlhelper you can add a class to textarealike

TextArea使用 ASP.NET MVC 的Html帮助程序进行渲染并使其可调整大小是两个不同的关注点。使用Html助手时,您可以添加一个类来textarea喜欢

<%:Html.TextAreaFor(x => x.SomeProperty, new { @class = "resizer" }) %>

Then you can hook this class with jQuery to make it resizable when it's rendered on the web page. Please refer to Implementing a resizable textarea?for information about making your textarearesizable.

然后你可以用 jQuery 钩住这个类,让它在网页上呈现时可以调整大小。请参阅实现可调整大小的文本区域?有关使您的textarea可调整大小的信息。

回答by GleDel

Use the code below when using Razor

使用 Razor 时使用以下代码

   @Html.TextAreaFor(model => model.Comments, 10, 40, null);

回答by Zarko Eeternal

Even better, if you want auto-calculate number of the rows in the textarea you can use this

更好的是,如果您想自动计算 textarea 中的行数,您可以使用它

@Html.TextAreaFor(m => m.Comments, Model.Comments.Count + 1, 40, null)