MVC模式需要验证不工作的所有领域 [英] mvc model validation required not working on all fields

查看:205
本文介绍了MVC模式需要验证不工作的所有领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC 4个工作,我有我的模型验证无法正常工作的问题。出于某种原因,不是所有我需要的领域都需要填写。

I'm working in ASP.NET MVC 4 and I have the problem that my model validation isn't working correctly. For some reason not all my required fields have to be filled in.

下面是我的模型:

public class MovieModel
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public DateTime ReleaseDate { get; set; }
        [Required]
        public string Genre { get; set; }
        [Required]
        public decimal Price { get; set; }

        public virtual ICollection<RoleInMovie> RoleInMovie { get; set; }
    }

这里的景观:

@using (Html.BeginForm())
{
    <table>
        <tr>
            <td>
                <label>Name:</label></td>
            <td>@Html.EditorFor(m => m.Name)</td>
            <td>@Html.ValidationMessageFor(m => m.Name)</td>
        </tr>
        <tr>
            <td>
                <label>Genre:</label></td>
            <td>@Html.EditorFor(m => m.Genre)</td>
            <td>@Html.ValidationMessageFor(m => m.Genre)</td>
        </tr>
        <tr>
            <td>
                <label>Price:</label></td>
            <td>@Html.EditorFor(m => m.Price)</td>
            <td>@Html.ValidationMessageFor(m => m.Price)</td>
        </tr>
    </table>
    <button type="submit">Submit</button>
}

这是我的动作:

[HttpPost]
        public ActionResult Add(MovieModel model)
        {
            if(ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View();
        }

现在这里的东西:只要我只输入一个价格,modelstate.isvalid变为真。当鼠标悬停在我的模型,它最高审计机关既名称和类型都为空。 Ofcourse它们是必需的,但验证不工作。
此外,validationmessagefor只能在价格上。

Now here's the thing: as soon as I enter only a price, modelstate.isvalid becomes true. When hovering over my model, it sais both name and genre are null. Ofcourse they are required, but the validation doesn't work. Also, the validationmessagefor only works on price.

我希望我不是忽视的东西太可笑了。感谢您的帮助!

I hope I'm not overlooking something too ridiculous. Thanks for the help!

推荐答案

返回无效的模型回到观点:

Return the invalid model back to the view:

[HttpPost]
public ActionResult Add(MovieModel model)
{
    if(ModelState.IsValid)
    {
        return RedirectToAction("Index");
    }
    return View(model); // <----
}

呵呵,并确保所需要的属性是不允许空字符串

Oh, and make sure that the required attribute is disallowing empty strings

<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.allowemptystrings.aspx\">http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.allowemptystrings.aspx

public class MovieModel
{
    public int Id { get; set; }

    [Required(AllowEmptyStrings = false)]
    public string Name { get; set; }

    public DateTime ReleaseDate { get; set; }

    [Required(AllowEmptyStrings = false)]
    public string Genre { get; set; }

    [Required]
    public decimal Price { get; set; }

    public virtual ICollection<RoleInMovie> RoleInMovie { get; set; }
}

这篇关于MVC模式需要验证不工作的所有领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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