1

我有一个表格:

using (Html.BeginForm())
{
  @Html.TextBoxFor(x => x.Name, new { @class = "span8" })
}

模型:

[Required]
[RegularExpressionAttribute("regex")]
public string Name { get; set; }

控制器:

[HttpPost]
public ActionResult Index(MyModel model)
{
    return View(model);
}

如果禁用 javascript,如何显示错误消息并更改名称文本框的 css 样式?

据我了解:

@Html.ValidationMessageFor(x => x.Name, "error text", new { style = "backgroundcolor: red;"})

不行吗?

在输出 HTML 中,我可以看到生成的 span,ValidatorMessageFor但它有一个btn-group隐藏此标签的 css 类。

4

1 回答 1

0

You should add modelstate validation errors from controller and then redisplay the form. It's possible without using javascript

ModelState.AddModelError("elementName", "Error definition");
于 2012-12-21T09:39:13.663 回答