Pregunta

I have a form in which I want to disable the required validation on one of the @Html.TextBox elements. The front end is as follows:

@model Models.NewUser

@using (Html.BeginForm("Register", "Account"))
{
    <div class="field">
        <span class="textOverField absolute em1-2" style="top:6px;left:4px;">Key</span>
        @Html.TextBox("SchoolGroupKey", "", new { @class = "textField" }) 
    </div>
    <div class="field">
        <span class="textOverField absolute em1-2" style="top:6px;left:4px;">Name</span>
         @Html.TextBox("Names", "", new { @class = "textField" }) <div class="absolute validationMessage">@Html.ValidationMessageFor(u => u.Names)</div>
    </div>
    <div class="field">
        <span class="textOverField absolute em1-2" style="top:6px;left:4px;">Last Name</span>
        @Html.TextBox("LastNames", "", new { @class = "textField" }) <div class="absolute validationMessage">@Html.ValidationMessageFor(u => u.LastNames)</div>
    </div>
    <div class="em0-9" style="margin-bottom:20px;">
        <input class="submitButton cursorHand" style="margin-top:10px;" type="submit" value="Registrarme" />
    </div>
}

And the code for the model is as follows:

public class NewUser
{
    [Required(ErrorMessage = "Name required")]
    public string Names { get; set; }

    [Required(ErrorMessage = "Last name required")]
    public string LastNames { get; set; }

    public System.Guid SchoolGroupKey { get; set; }
}

I'm guessing that the @Html.TextBox has a default validation even though I don't specify it. Is there someway to remove it?, I tried removing the html tags data-val-required and data-val, but still no results.

Thanks

¿Fue útil?

Solución

Guid is a structure that behave as a simple type (just like int or DateTime). As such it is automaticly validated. If you want to make a workaround change your property type from Guid to nullable Guid?. Example:

public System.Guid? SchoolGroupKey { get; set; }

Of course you will have to change places when you use this property, to access the guid value throught Value property of nullable type.

If the mapping fails you will not get any validation errors then - just null value inside of your SchoolGroupKey property.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top