Correcting lost form values when a comment is not valid

Work Item: 16817

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-11-22 15:26:29 -08:00
parent fe9225ce8a
commit 1918a78646
2 changed files with 31 additions and 15 deletions

View File

@@ -7,8 +7,6 @@ using Orchard.Comments.ViewModels;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.Localization; using Orchard.Localization;
using Orchard.UI.Notify; using Orchard.UI.Notify;
using Orchard.Utility.Extensions;
using System.Text.RegularExpressions;
namespace Orchard.Comments.Controllers { namespace Orchard.Comments.Controllers {
public class CommentController : Controller { public class CommentController : Controller {
@@ -33,16 +31,19 @@ namespace Orchard.Comments.Controllers {
: Redirect("~/"); : Redirect("~/");
var viewModel = new CommentsCreateViewModel(); var viewModel = new CommentsCreateViewModel();
if (TryUpdateModel(viewModel)) {
var context = new CreateCommentContext {
Author = viewModel.Name,
CommentText = viewModel.CommentText,
Email = viewModel.Email,
SiteName = viewModel.SiteName,
CommentedOn = viewModel.CommentedOn
};
TryUpdateModel(viewModel);
var context = new CreateCommentContext {
Author = viewModel.Name,
CommentText = viewModel.CommentText,
Email = viewModel.Email,
SiteName = viewModel.SiteName,
CommentedOn = viewModel.CommentedOn
};
if (ModelState.IsValid) {
if (!String.IsNullOrEmpty(context.SiteName) && !context.SiteName.StartsWith("http://") && !context.SiteName.StartsWith("https://")) { if (!String.IsNullOrEmpty(context.SiteName) && !context.SiteName.StartsWith("http://") && !context.SiteName.StartsWith("https://")) {
context.SiteName = "http://" + context.SiteName; context.SiteName = "http://" + context.SiteName;
} }
@@ -58,6 +59,13 @@ namespace Orchard.Comments.Controllers {
} }
} }
if(!ModelState.IsValid) {
TempData["CreateCommentContext.Name"] = context.Author;
TempData["CreateCommentContext.CommentText"] = context.CommentText;
TempData["CreateCommentContext.Email"] = context.Email;
TempData["CreateCommentContext.SiteName"] = context.SiteName;
}
return !String.IsNullOrEmpty(returnUrl) return !String.IsNullOrEmpty(returnUrl)
? Redirect(returnUrl) ? Redirect(returnUrl)
: Redirect("~/"); : Redirect("~/");

View File

@@ -3,6 +3,14 @@
@using Orchard.Security; @using Orchard.Security;
@using Orchard.Utility.Extensions; @using Orchard.Utility.Extensions;
@{
var contextExists = TempData["CreateCommentContext.Name"] != null;
var name = Convert.ToString(TempData["CreateCommentContext.Name"]);
var commentText = Convert.ToString(TempData["CreateCommentContext.CommentText"]);
var email = Convert.ToString(TempData["CreateCommentContext.Email"]);
var siteName = Convert.ToString(TempData["CreateCommentContext.SiteName"]);
}
@if (Model.ContentPart.Comments.Count > 0) { @if (Model.ContentPart.Comments.Count > 0) {
<div id="comments"> <div id="comments">
<h2 class="comment-count">@T.Plural("1 Comment", "{0} Comments", (int)Model.ContentPart.Comments.Count)</h2> <h2 class="comment-count">@T.Plural("1 Comment", "{0} Comments", (int)Model.ContentPart.Comments.Count)</h2>
@@ -30,15 +38,15 @@ using (Html.BeginForm("Create", "Comment", new { area = "Orchard.Comments" }, Fo
<ol> <ol>
<li> <li>
<label for="Name">@T("Name")</label> <label for="Name">@T("Name")</label>
<input id="Name" class="text" name="Name" type="text" /> <input id="Name" class="text" name="Name" type="text" value="@(contextExists ? name : String.Empty)" />
</li> </li>
<li> <li>
<label for="Email">@T("Email")</label> <label for="Email">@T("Email")</label>
<input id="Email" class="text" name="Email" type="text" /> <input id="Email" class="text" name="Email" type="text" value="@(contextExists ? email : String.Empty)"/>
</li> </li>
<li> <li>
<label for="SiteName">@T("Url")</label> <label for="SiteName">@T("Url")</label>
<input id="SiteName" class="text" name="SiteName" type="text" /> <input id="SiteName" class="text" name="SiteName" type="text" value="@(contextExists ? siteName : String.Empty)"/>
</li> </li>
</ol> </ol>
</fieldset> </fieldset>
@@ -52,7 +60,7 @@ using (Html.BeginForm("Create", "Comment", new { area = "Orchard.Comments" }, Fo
<ol> <ol>
<li> <li>
<label for="comment-text">@T("Comment")</label> <label for="comment-text">@T("Comment")</label>
<textarea id="comment-text" rows="10" cols="30" name="CommentText"></textarea> <textarea id="comment-text" rows="10" cols="30" name="CommentText">@(contextExists ? commentText : String.Empty)</textarea>
</li> </li>
<li> <li>
<button class="primaryAction" type="submit">@T("Submit Comment")</button> <button class="primaryAction" type="submit">@T("Submit Comment")</button>