-1

I have discovered the Foolproof library that seems very nice but I am having problems to put it working.

I want to create a required field only if the dropdown has the selected value = 7.

Simple Model:

[RequiredIf("LeadSource_Id","7", ErrorMessage = "Campo obrigatório")]
public string SourceDescription { get; set; }

[Display(Name = "Origem")]
public virtual int LeadSource_Id { get; set; }

The way I create the Dropdown in the Controller:

 ViewBag.LeadSource_Id = new SelectList(db.LeadSources.ToList(), "Id", "Name");

The View:

<div class="form-group">
    @Html.LabelFor(model => model.LeadSource_Id, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("LeadSource_Id", null, htmlAttributes: new { @class = "form-control ld-lead-source" })
        @Html.ValidationMessageFor(model => model.LeadSource_Id, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group collapse">
    @Html.LabelFor(model => model.SourceDescription, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.SourceDescription, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.SourceDescription, "", new { @class = "text-danger" })
    </div>
</div>

When I try to see if the validation is working when I select the value 7, I get the error:

The ViewData item that has the key 'LeadSource_Id' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

EDIT:

The libraries I include are:

<script src="~/Scripts/jquery/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery/jquery.validate.globalize.min.js"></script>
<script src="~/Scripts/mvcfoolproof.unobtrusive.min.js"></script>
Patrick
  • 2,995
  • 14
  • 64
  • 125

1 Answers1

1

The error occurs because the value ofViewBag.LeadSource_Id is null. Since you have set its value in the GET method, then presumably this error is occurring when you return the view in your POST method (which you have omitted) but have not reassigned the value. In addition you cannot give the ViewBag property the same name as your model property.

Change your controller code to (say)

ViewBag.LeadSourceList = new SelectList(db.LeadSources.ToList(), "Id", "Name");

and ensure this code appears in both the GET method and POST method is you return the view, and modify the view to

@Html.DropDownList("LeadSource_Id", IEnumerable<SelectListItem>ViewBag.LeadSourceList , { @class = "form-control" })

However the recommended approach is to use a view model containing a property public IEnumerable<SelectListItem> LeadSourceList { get; set;}

  • There would be no issue from framework if the model property and ViewBag dynamic property name is same. As model property is used to create the input control to have the Model binding name. The answer given by you to change the collection parameter to SelectList is also given by me. However, your have a syntax error `@Html.DropDownList("LeadSource_Id", IEnumerableViewBag.LeadSourceList , @class = "form-control" })`. If you notice you missed the `{` before @class for html attributes declaration. My apologies, if it was treated as revenge vote. I have voted up again. – user1672994 Dec 15 '15 at 04:49
  • @user1672994 Thanks for pointing out the typo (now fixed). There are problems with giving both the property your binding to and the `ViewBag` property the same name and there are numerous question/answers relating to this on SO (e.g. [this one](http://stackoverflow.com/questions/19476530/html-dropdownlistfor-selected-value-not-being-set)). But the issue is not the name, or the whether the second parameter is a `SelectList`, its that the value of `ViewBag.LeadSource_Id` is `null`, which means that the `DropDownListFor()` method expects the model property to be a `SelectList` (which its not) –  Dec 15 '15 at 05:05