1

I am using asp.net mvc to do model binding. When I pass a model to a view I am able to see the model data displayed in the form inside a label

 <%= Html.Label(Model.title) %>
 <%= Html.Label(Model.description) %>

However I am not able to do the same using

 <%= Html.TextArea(Model.description)%>

Is there a syntax difference between displaying in a label as oppsed to a textbox?

Here is my view

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<EditDocumentViewData>" %>


<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Update
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" 
    <h2>Update</h2>
      <form id="myForm"  action="<%=Url.Action("Update") %>"  method="post" >  
   <% Html.EnableClientValidation(); %>
        <div id="validationSummary"><%= Html.ValidationSummary() %> </div>
    <%= Html.ClientSideValidation(typeof(Document))
        .UseValidationSummary("validationSummary") %>
<div style="float:left">

<input type="button" class="btnpost" id="btnMain" value="Main Thumb"/>
    <input id="btnDelete"  class="btnpost" type="button"  value="Delete"/>
    <br />   <br />   <br />

   <br />   <br />   <br />   <br />

    <table>
      <%= Html.HiddenFor(m => m.id)%>

       <tr>   <td> <%=Html.Label("Title")%></td><td>
    <%=Html.TextBox("title", Model.title)%>
    </td> </tr>
      <tr>   <td> <%=Html.Label("Description")%></td><td>
 <%= Html.TextArea("description", Model.description)%>

     </td> 
     <td>
                    <%= Html.ValidationMessage("description")%>

            </td>
     </tr><tr><td> <%=Html.Label("Summary")%></td><td>


   <%= Html.TextAreaFor(m=>m.summary)%>

     </td>  <td>
                    <%= Html.ValidationMessage("summary")%>

            </td></tr>


  </form>    


</asp:Content>

my contollers actions are

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Update(int Id)
{
    Document doc= _documentRepository.GetById(Id);
    EditDocumentViewData documentViewData=new EditDocumentViewData();
    documentViewData.id = doc.document_id;
    documentViewData.category = doc.Category1.name;
    documentViewData.title = doc.title;
    documentViewData.Thumbs = doc.Thumbs.ToList();
    documentViewData.description = doc.description;

    documentViewData.summary = doc.summary;

    return View(documentViewData);
    // TempData["docid"] = doc.document_id;
    //if (doc  != null)
    //    return View(doc);

    //else
    //    return View("Index");

}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Update(EditDocumentViewData editDoc)
{
    Document doc= _documentRepository.GetById(editDoc.id);

    doc.title = editDoc.title;

    doc.description = editDoc.description;

    doc.summary = editDoc.summary;
    _unitOfWorkManager.Commit();
    return RedirectToAction("Index");
}
1
  • 1
    Don't tell me you're using Html.Label() to display arbitrary data. They're for input labels, you know Commented May 18, 2010 at 21:54

2 Answers 2

3

In your usage, the first two create label elements and use the argument for the both the text of the label and the label's for property. The third will create a textarea, but it uses the argument as the name for the area. If you want the textarea to contain the contents of the description, you need to use a different signature.

<%= Html.TextArea( "Description", Model.Description ) %>

or use the strongly-typed helper

<%= Html.TextAreaFor( m => m.Description ) %>

On another node, if you simply want to display the contents of the model property, you should be using Encode or the newer <%: %> syntax (in ASP.NET 4).

<span class="description">
<%= Html.Encode( Model.Description ) %>
</span>
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry I just noticed that on postback, all my fields were null, do you know why this happens? Thanks
@mctayl - what does your action signature look like? Do you have the same model in the post action signature or named parameters that match the models properties? You also need to wrap all the inputs in a form (obviously) -- I only mention it because you haven't included all your code and creating the form is very different in MVC vs. web forms.
Thanks fpr getting back to me, I have posted the code above, the validation does not work and the model is null when the controller action is called, I understand there is no postback in mvc, it was a misuse of the word by myself :)
@mctayl - The client-side validation isn't enabled because you're not using the HtmlHelper BeginForm extension to generate the form. You need to enable validation before using the helper to generate the form. It's in the form generate that the necessary glue is written to the page to make the validation work. Not sure why the parameters aren't being bound. Have you verified using Fiddler or Firefox/Firebug that the request contains the form values when it's posted?
I dont think the HtmlHelper BeginForm is the problem, because i am able to get te validation working in another view. I suspect the model binding issues and the validation issues are related and yes I have checked firebug and the name attribute of the elements are set to the properties of the model
|
2
    <%= Html.TextAreaFor(model => model.Description, 4, 10, new { style = "width: 100%", @class = "textarea" })%>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.