Jun 24, 2013, 9:58 AM
ASP.NET MVC ModelState Error display
Hello!
Could you help me please with displaying ModelState error on my form?
I've been struggling with this for two days still no luck.
Model
How can I get something like this?
Thanks.
Could you help me please with displaying ModelState error on my form?
I've been struggling with this for two days still no luck.
Model
public class PostModel
{
[Required]
public string Author { get; set; }
[Required]
[StringLength(20, ErrorMessage = "Message should be from 5 to 20 characters", MinimumLength = 5)]
public string Message { get; set; }
}
Controllerpublic class BlogController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult PostMessage(PostModel post)
{
if (ModelState.IsValid)
{
bool saveSucceed = Repository.Save(post);
if(!saveSucceed) ModelState.AddModelError("", "New post save failed");
}
return this.FormPanel(ModelState);
}
}
View@model SampleExt.Models.PostModel
@(Html.X().ResourceManager())
@(Html.X().Window()
.Height(150)
.Icon(Icon.Add)
.Title("Add Post")
.Draggable(true)
.Width(350)
.BodyPadding(5)
.Layout(LayoutType.Fit)
.Items(
Html.X().FormPanel()
.ID("PostForm1")
.Layout(LayoutType.Form)
.Border(false)
.BodyStyle("background-color:transparent;")
.Listeners(ls =>
{
ls.ValidityChange.Handler = "#{Button1}.setDisabled(!valid);";
})
.FieldDefaults(def =>
{
def.MsgTarget = MessageTarget.Qtip;
})
.Items(
Html.X().TextFieldFor(m=>m.Author).LabelAlign(LabelAlign.Top),
Html.X().TextAreaFor(m=>m.Message).LabelAlign(LabelAlign.Top)
)
.Buttons(
Html.X().Button()
.ID("Button1")
.Text("Post it")
.Disabled(true)
.Icon(Icon.Accept)
.DirectEvents(de => {
de.Click.Url = Url.Action("PostMessage");
de.Click.FormID = "PostForm1";
})
)
)
)
For now I've got this error displayed like thisHow can I get something like this?
Thanks.