禁用需要确认特定字段在视图ASP.NET MVC 4 [英] Disable Required Validation Specific Field in the View ASP.NET MVC 4

查看:222
本文介绍了禁用需要确认特定字段在视图ASP.NET MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以给我一些暗示我会AP preciate。

if someone could give me some hint I would appreciate.

我在寻找了一段时间,我甚至找到了一个职位,我认为这将解决我的问题,但事实并非如此。

I'm searching for a while, and I even found a post I thought it would solve my problem, but it didn't.

<一个href=\"http://stackoverflow.com/questions/5367287/disable-required-validation-attribute-under-certain-circumstances\">Disable在某些情况下需要验证属性

基本上我有一个简单的模型User.cs在那里我的用户名, SignupDate

Basically I have a simple User.cs model where I have username, FirstName, LastName and SignupDate

所有具备必要的注释,我想解决这个问题,而无需删除的必需标记。

All have the required annotation and I would like to solve this without erasing the Required tag.

在我生成视图,我在视图中删除HTML code为 SignupDate

After I generate the view, I erase in the view the html code for the SignupDate:

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

当我点击提交这是行不通的。

When I click submit it does not work.

另外,如果我不建议在其他职位

Also if I do the suggested in the other post

<div class="editor-label">
        @Html.LabelFor(model => model.SignupDate)
</div>
<div class="editor-field">
       @Html.TexBoxFor(model => model.SignupDate, new { data_val = false })
</div>

如果我离开它,因为空白也不起作用。

If I leave it as blank also does not work..

有什么建议?谢谢!

推荐答案

您可以禁用View客户端验证,并删除对这些实体您不想验证的值的ModelState中的错误。

You can disable client validations on the view and remove the errors on the modelstate for those entities you don't want to validate the value.

在我来说,我想改变只有当用户输入一个密码。使用Html.HiddenFor不是一个好方法,由于每次将密码发送到客户端,和密码不应该被发送。

In my case I wanted to change a Password only if the user typed one. Using Html.HiddenFor was not a good approach due to sends the password to the client every time, and password shouldn't be sent.

我所做的就是禁用视图上的客户端验证

What I did was to disable the client validations on the view

@model MyProject.Models.ExistingModelWithRequiredFields

@{
    ViewBag.Title = "Edit";
    Html.EnableClientValidation(false);
}

这让我提交表单,即使空值。请注意,所有客户端验证都将被忽略,但是服务器的验证仍然运行,所以你需要清除那些你不需要执行。为了做到这一点,到控制器中的作用,并删除错误的每个需要属性

That allows me to submit the form even with empty values. Please note that all client validations are ignored, however server validations still run, so you need to clear those you don't need to be executed. In order to do this, go to the action in the controller and remove the errors for each property you need to

public ActionResult Edit(ExistingModelWithRequiredFields updatedModel)
{
    var valueToClean = ModelState["RequiredPropertyName"];
    valueToClean.Errors.Clear(); 
    if(ModelState.IsValid)
    {
        ...
        //Optionally you could run validations again 
        if(TryValidateModel(updatedModel)
        {
            ...
        }
        ...
    }
    ...
}

这篇关于禁用需要确认特定字段在视图ASP.NET MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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