This is a migrated thread and some comments may be shown as answers.

[Solved] RadEditor and asp.net MVC dataannotation?

1 Answer 36 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Cha Garcia
Top achievements
Rank 1
Cha Garcia asked on 04 May 2010, 09:47 PM

I am using DataAnnotation to validate my form in my view. My problem is the content from radeditor is ignored by DataAnnotations.
When I call  if (ModelState.IsValid) {}, it is always false?  Is there a work around to validate the radeditor along with the fields validated using dataAnnotations and ModelState.IsValid?  Additional note:  I have no problem getting the content from the radeditor using the code from: http://www.telerik.com/help/aspnet-ajax/mvc-using-editor.html.

My code:

Controller
==============

 [HttpPost]
        [SessionExpireFilter]
        public ActionResult Edit(Guid id, Letter letter)
        {

            // TODO: Add update logic here
            string editorBody = RadHelper.ExtractStringValue(Request, "CommentsEditor");
            letter.Body = editorBody;

            if (ModelState.IsValid)
            {
               
                ValidationFault theFault = null;
                var theClient = PCIIService.WorkflowClient();

                var theList = theClient.GetLetterTemplate(out theFault, id);
                var theLetter = theList.FirstOrDefault();

                theLetter.LetterTemplateId = id;
                theLetter.Name = letter.Name;
                theLetter.Body = letter.Body;
                theClient.UpdateLetterTemplate(out theFault, theLetter);
                return RedirectToAction("Index");
            }

            // Invalid
            return View(letter);
        }

    }


Model
================

namespace PCIIMSWorkflow.Models
{
    public class Letter
    {
        public Guid LetterTemplateId { get; set; }

        [Required(ErrorMessage = "Name is Required ")]
        [StringLength(100, ErrorMessage = "Must be under 100 characters ")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Body is Required ")]
        public string Body { get; set; }

    }
}



View
=================

<%@ Page Title="" Language="C#" MasterPageFile="~/PCIIOpsPage.Master" Inherits="System.Web.Mvc.ViewPage<PCIIMSWorkflow.Models.Letter>" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />


<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

    <% using (Html.BeginForm()) {%>

  <h3>Edit</h3>
  <h2><%= Html.Encode(ViewData["Message"]) %></h2>
 <form method="post" action ="/Letter/Edit/" runat="server">
 <table>
<tr><td><%=Html.Label("Letter Name")%> </td><td> <%=Html.TextBox("Name", Model.Name) %> <%= Html.ValidationMessageFor(model => model.Name) %></td></tr>
<tr><td colspan="2">
<%CommentsEditor.Content = Model.Body; %>
<asp:ScriptManager ID="ScriptManager" runat="server" />
<telerik:RadEditor ID="CommentsEditor"  Runat="server" DialogHandlerUrl="~/Telerik.Web.UI.DialogHandler.axd"  StripFormattingOptions="MSWord" ToolbarMode="Default" Height="500px">
<Content>

</Content>
                                      
</telerik:RadEditor> <%= Html.ValidationMessageFor(model => model.Body) %>

   <p>
                <input type="submit" value="Save" />
            </p>
</td></tr>                                        
</table>
</form>


       
               

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 05 May 2010, 07:33 AM
Hi Cha Garcia,

Unfortunately this is true. The content of RadEditor cannot be automatically validated - you have to do this manually. The reason is RadEditor does not output an input field whose name attribute matches the ID of the component. This is required for proper ASP.NET MVC validation.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
General Discussions
Asked by
Cha Garcia
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or