我有一个区域,在这个区域我有一个 PostController,一个为 GET 方法添加名称的操作和一个为 POST 方法添加名称的操作,在提交表单后我将错误添加到模型状态,但在回发后,没有显示任何验证消息状态码是:301 永久移动!!!
我的视图模型:
public class NewPostViewModel
{
[Required(ErrorMessage = "Please enter title.")]
public string Title { get; set; }
[AllowHtml]
[Required(ErrorMessage = "Please enter article content.")]
public string Content { get; set; }
[Required(ErrorMessage = "Please enter a tag.")]
public string Tags { get; set; }
public bool PublishNow { get; set; }
public string PublishDate { get; set; }
public string PublishTime { get; set; }
}
我的观点:
@model Soleimanzadeh.Models.ViewModels.NewPostViewModel
@{
ViewBag.Title = "Add";
Layout = MVC.Admin.Shared.Views.BaseLayout;
}
@using (Html.BeginForm(MVC.Admin.Post.Add(), FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(false, null, new { @class = "alert" })
<div class="row-fluid">
<div class="span9">
<div class="row-fluid">
<div class="span8">
@Html.TextBoxFor(np => np.Title, new { @class = "span12", placeholder = "Title" })
@Html.ValidationMessageFor(model=>model.Title)
</div>
</div>
<div class="row-fluid">
<div class="span12">
@Html.TextAreaFor(np => np.Content)
@Html.ValidationMessageFor(model=>model.Content)
</div>
</div>
</div>
<div class="span3">
<div class="row-fluid">
<div class="post-options-container">
<div class="header">
Tags
</div>
<div class="content">
<input type="text" placeholder="tags" class="tagManager" style="width: 100px;" />
</div>
</div>
</div>
<div class="row-fluid">
<div class="post-options-container">
<div class="header">
Status
</div>
<div class="content">
<label class="checkbox">
@Html.CheckBoxFor(np => np.PublishNow)
Publish Now?
</label>
<div>
Publish Date:
@Html.TextBoxFor(np => np.PublishDate, new { @class = "calcInput" })
</div>
<div>
Publish Time:
@Html.TextBoxFor(np => np.PublishTime, new { @class = "timeInput" })
</div>
<div>
<input type="submit" value="Save"/>
</div>
</div>
</div>
</div>
</div>
</div>
}
@*some scripts here*@
我的控制器:
public partial class PostController : Controller
{
public virtual ActionResult Add()
{
var newPost = new NewPostViewModel();
return View(MVC.Admin.Post.Views.Add, newPost);
}
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Add(NewPostViewModel post)
{
ModelState.AddModelError("Content","Wrong");
return this.View(MVC.Admin.Post.Views.Add, post);
}
}
我还为内容使用了redactor html 编辑器。
解决方案:
我使用http://lowercaseroutesmvc.codeplex.com/将 url 转换为小写,但我没有在区域部分启用此工具,并在提交表单后重定向页面。