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

MAS
  • 43
  • 6

3 Answers3

1

Just make value= false in web.config for ClientValidationEnabled

<appSettings><add key="ClientValidationEnabled" value="false" /></appSettings>
Vishal modi
  • 1,571
  • 2
  • 12
  • 20
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); }
Ravi Mali
  • 119
  • 9
  • ddn't work :' the message still showing, that make me cant submit the form – MAS Nov 01 '19 at 07:04
  • did you write @Html.ValidationMessage for country? or it is giving system generated message. if you have written it you should remove that. – Ravi Mali Nov 01 '19 at 07:33
  • 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" }) – Ravi Mali Nov 01 '19 at 07:44
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"),
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • this is my CountryLists Data Model public class HEM002 { public string CountryName { get; set; } public string CountryCode { get; set; } public string CountryAcr { get; set; } } – MAS Nov 01 '19 at 09:39