0

I have a dropdown list and a datefield. If there is one specific value selected from the dropdown, the datefield should be required. To solve that, I tried to use the requiredIf sttribute from the FoolProof library.

But I don't know how to access the selected value in the Model.

Model

//This is the datefield
[RequiredIf(tabState == "discarded")]
[DataType(DataType.Date)]
[Display(Name = "date discarded")]
public Nullable<System.DateTime> datDiscarded { get; set; }

//This is the dropdown
public virtual tabState tabState { get; set; }

tabState in Controller

ViewBag.intIdeaStateID = new SelectList(db.tabState, "intStateID", "strState");

dropdown in View

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

How can I check if the selected value from the dropdown is "discarded"?

Thanks for your help.

  • What is `tabState`? You cant bind a dropdown to a complex object (tabState) - you need a value type property to bind to (e.g. `int`, `string` etc. –  Jan 29 '16 at 09:51
  • 1
    Still makes no sense. The syntax is `[RequiredIf("tabState", "discarded")]` but it would never work anyway because your property `tabState` can never have a value `"discarded"` (but a property of `tabState` might) –  Jan 29 '16 at 10:01
  • @StephenMuecke That's my problem, I don't know how I can access the value or what the value would be... – M.Zuberbühler Jan 29 '16 at 10:05
  • Edit the question to show how you generate the dropdownlist in the view so I can correct that as well. And can you confirm the one of the options your generating has ` –  Jan 29 '16 at 10:10
  • @StephenMuecke The option discarded doesn't have to exist. I just want to know IF the selected value is "discarded" or the ID of "discarded" (the dropdown is generated from tabState and discarded could be one of the entries) – M.Zuberbühler Jan 29 '16 at 10:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101990/discussion-between-m-zuberbuhler-and-stephen-muecke). – M.Zuberbühler Jan 29 '16 at 10:21

0 Answers0