Html.TextAreaFor in asp.net mvc

只愿长相守 提交于 2019-12-09 02:29:34

问题


I have a asp.net mvc view that allows the user to enter some description in a 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?

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

回答1:


Rendering TextArea using ASP.NET MVC's Html helper and making it resizable are two different concerns. When using Html helper you can add a class to textarea like

<%: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 textarea resizable.




回答2:


Use the code below when using Razor

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



回答3:


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

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


来源:https://stackoverflow.com/questions/5893171/html-textareafor-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!