@ Html.ValidationMessageFor不起作用 [英] @Html.ValidationMessageFor not working

查看:53
本文介绍了@ Html.ValidationMessageFor不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在Google和Yahoo上看过,但找不到答案来解释为什么它是我的"@ Html.ValidationMessageFor不起作用".当我运行该应用程序时,没有任何反应,它允许输入所有内容.当我尝试在下面的编辑视图中编辑项目时,它也会崩溃.我有以下内容:

Hi guys I have looked on Google, yahoo and could not locate the answer to why it is my '@Html.ValidationMessageFor does not work.' When i run the application nothing happens it allows everything to be entered. And it also crashes when I try to edit a item in my edit view which is below. I have the following:

<div class="editor-label">
       @* @Html.LabelFor(model => model.Posted)*@
    </div>
    <div class="editor-field">
        @Html.HiddenFor(model => model.Posted, Model.Posted = DateTime.Now)
        @Html.ValidationMessageFor(model => model.sendinghome)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Cartypes)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Cartypes)
        @Html.ValidationMessageFor(model => model.Cartypes)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.RegNum)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.RegNum)
        @Html.ValidationMessageFor(model => model.RegNum)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Regprice)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Image)
        @Html.ValidationMessageFor(model => model.Regprice)
    </div>

推荐答案

以下是验证的工作方式.

Here is how validation works.

假设您有以下模型:

public class MyModel {
    [Required]
    public string MyProperty { get; set; }
}

请注意 Required 属性,它是一个数据注释属性,指定MyProperty是必填字段.

MyModel:

@model MyNamespace.MyModel

@using (Html.BeginForm("MyAction", "MyController")) {
    @Html.LabelFor(m => m.MyProperty)
    @Html.TextBoxFor(m => m.MyProperty)
    @Html.ValidationMessageFor(m => m.MyProperty)

    <input type="submit" value="Click me">
}

然后,当将此表单发布到MyControllerMyAction操作时,将执行模型验证.您要做的就是检查模型是否有效. 可以使用ModelState.IsValid属性来完成.

Then, when this form gets posted to the MyAction action of MyController, the validation of your model will be performed. What you have to do is check whether your model is valid or not. It can be done using the ModelState.IsValid property.

[HttpPost]
public ActionResult MyAction(MyModel model) {
    if (ModelState.IsValid) {
         // save to db, for instance
         return RedirectToAction("AnotherAction");
    }
    // model is not valid
    return View("MyView", model);
}

如果验证失败,将使用ModelState对象中存在的不同错误再次渲染视图.这些错误将由ValidationMessageFor帮助器使用和显示.

If the validation failed, the view will be rendered again using the different errors that are present in the ModelState object. Those errors will be used and displayed by the ValidationMessageFor helper.

这篇关于@ Html.ValidationMessageFor不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆