简体   繁体   English

在视图ASP.NET MVC 4中禁用所需的特定于验证的字段

[英]Disable Required Validation Specific Field in the View ASP.NET MVC 4

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. 我正在寻找一段时间,我甚至发现了一个帖子,我认为它可以解决我的问题,但事实并非如此。

Disable Required validation attribute under certain circumstances 在某些情况下禁用必需的验证属性

Basically I have a simple User.cs model where I have username, FirstName , LastName and SignupDate 基本上我有一个简单的User.cs模型,其中我有用户名, FirstNameLastNameSignupDate

All have the required annotation and I would like to solve this without erasing the Required tag. 所有都有必要的注释,我想解决这个问题而不删除必需的标记。

After I generate the view, I erase in the view the html code for the SignupDate : 生成视图后,我在视图中删除SignupDate的html代码:

 <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.. 如果我把它留空也不起作用..

Any suggestions? 有什么建议么? Thanks!! 谢谢!!

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. 您可以在视图上禁用客户端验证,并删除模型状态中的错误,以查找您不想验证该值的实体。

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. 使用Html.HiddenFor不是一个好方法,因为每次都会向客户端发送密码,并且不应发送密码。

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)
        {
            ...
        }
        ...
    }
    ...
}

我认为这应该解决它,假设model.SignupDate持有一个值:

<%: Html.HiddenFor(model => model.SignupDate) %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM