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>