0

i've a form that some of the fields is not required to be filled or optional fields in asp.net mvc 5 application.

i've tried this things but, warning message for "this fields is required", keep showing.

[Required(AllowEmptyStrings = true)]
public string country { get; set; }

adding htmlAttribute
@required = false

Data Model

public class LoginViewModel
{
    ...

    public string country { get; set; }

    ...
}

public class CountryLists
{
    ...
    public string CountryName { get; set; }
    public string CountryCode { get; set; }
    ...
}

Index.cshtml

@using (Html.BeginForm("Save", "SignUp", FormMethod.Post, new { name = "signUpForms", id = "signUpForm", @class = "registerLogin-form" }))
{
...
    if (Model.MembershipProgram.StsSignUpCountry)
    {
        <div class="form-group col-12">
           @Html.DropDownListFor(m => m.country, 
          new SelectList(Model.CountryLists, "CountryCode", "CountryName"),
          "Select Country",
            new
            {
                id = "select_country",
                @class = "form-control"
            })
        </div>
    }
...
<button type="submit" id="register-submit-btn" class="btn btn-primary pull-right active" name="command" value="Save">
            @ViewBag.JoinNow <i class="m-icon-swapright m-icon-white"></i>
</button>
}

enter image description here

6
  • Remove that [Required(AllowEmptyStrings = true)] Commented Nov 1, 2019 at 6:43
  • yes i was removed that, but the warning keep showing :\. before i dont use that Commented Nov 1, 2019 at 6:45
  • can you post the html which is rendered in your browser? Commented Nov 1, 2019 at 7:55
  • <div class="form-group col-12"> <select class="form-control" data-val="true" data-val-required="The country field is required." id="select_country" name="country"> <option value="">Select Country</option> <option value="CO0169">Yemen</option> <option value="CO0170">Zambia</option> <option value="CO0171">Zimbabwe</option> ... </select> </div> Commented Nov 1, 2019 at 8:02
  • I think the problem is some how it is adding data-val="true" which is causing this issue. You need to check some where in your model from which you are binding it is written. Commented Nov 1, 2019 at 8:05

3 Answers 3

1

Just make value= false in web.config for ClientValidationEnabled

<appSettings><add key="ClientValidationEnabled" value="false" /></appSettings>
Sign up to request clarification or add additional context in comments.

Comments

0

might be duplicat of : ASP .NET MVC Disable Client Side Validation at Per-Field Level

may be it helps: If your are using MVC4 and latest version you can write it as

 @{ Html.EnableClientValidation(false); }
 if (Model.MembershipProgram.StsSignUpCountry)
 {
    <div class="form-group col-12">
       @Html.DropDownListFor(m => m.country, 
      new SelectList(Model.CountryLists, "CountryCode", "CountryName"),
      "Select Country",
        new
        {
            id = "select_country",
            @class = "form-control"
        })
    </div>
}
@{ Html.EnableClientValidation(true); }

3 Comments

ddn't work :' the message still showing, that make me cant submit the form
did you write @Html.ValidationMessage for country? or it is giving system generated message. if you have written it you should remove that.
try this @Html.DropDownListFor(m => m.country, new SelectList(Model.CountryLists, "CountryCode", "CountryName"), "Select Country", new { id = "select_country", data_val = "false", @class = "form-control" })
0

Problem could be CountryCode.
Check your model if it is an integer then it should be like

 public int? CountryCode{ get; set; }

You will have a model with two properties at least

  public int CountryCode{ get; set; }
  public string CountryName{ get; set; }

You have mentioned in your code as below

new SelectList(Model.CountryLists, "CountryCode", "CountryName"),

1 Comment

this is my CountryLists Data Model public class HEM002 { public string CountryName { get; set; } public string CountryCode { get; set; } public string CountryAcr { get; set; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.