views:

351

answers:

2

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");
}
A: 
    <%= Html.TextAreaFor(model => model.Description, 4, 10, new { style = "width: 100%", @class = "textarea" })%>
mare
just one sample usage..;)
mare
+3  A: 

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>
tvanfosson
Thank you tvanfosson. It worked like a charm :)
mctayl
Sorry I just noticed that on postback, all my fields were null, do you know why this happens? Thanks
mctayl
@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.
tvanfosson
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
@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?
tvanfosson
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
mctayl
I also get a script error every time the page loadsError: Element title is not in a form, could this also be related?
mctayl
@mctayl - You will still get server side validation, but I think you need to use the form helper (and include the MS validation JavaScript) to get client-side validation. It's important, too, that the EnableClientValidation call come before the form helper. You also have a call I'm not familiar with `ClientSideValidation, is that your own extension? It might be helpful to see the generated HTML -- perhaps you could post it on pastebin.
tvanfosson