Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 访问公共虚拟下拉列表值_Asp.net Mvc_Model_Virtual_Data Annotations_Foolproof Validation - Fatal编程技术网

Asp.net mvc 访问公共虚拟下拉列表值

Asp.net mvc 访问公共虚拟下拉列表值,asp.net-mvc,model,virtual,data-annotations,foolproof-validation,Asp.net Mvc,Model,Virtual,Data Annotations,Foolproof Validation,我有一个下拉列表和一个日期字段。如果从下拉列表中选择了一个特定值,则datefield应该是必需的。 为了解决这个问题,我尝试使用万无一失库中的requiredIf属性 但我不知道如何访问模型中的选定值 型号 //This is the datefield [RequiredIf(tabState == "discarded")] [DataType(DataType.Date)] [Display(Name = "date discarded")] public Nullable<Sys

我有一个下拉列表和一个日期字段。如果从下拉列表中选择了一个特定值,则datefield应该是必需的。 为了解决这个问题,我尝试使用万无一失库中的requiredIf属性

但我不知道如何访问模型中的选定值

型号

//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; }
视图中的下拉列表

<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>

@LabelFor(model=>model.intIdeaStateID,“State”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“intIdeaStateID”,null,htmlAttributes:new{@class=“formcontrol”})
@Html.ValidationMessageFor(model=>model.intIdeaStateID,“,new{@class=“text danger”})
如何检查下拉列表中的选定值是否已“丢弃”


谢谢你的帮助。

什么是
tabState
?您无法将下拉列表绑定到复杂对象(tabState)-您需要绑定一个值类型属性(例如,
int
string
等)。仍然没有意义。语法是
[RequiredIf(“tabState”,“discarded”)]
,但它无论如何都不会工作,因为您的属性
tabState
永远不会有值
“discarded”
(但是
tabState
的属性可能会)@StephenMuecke这是我的问题,我不知道如何访问该值或该值是什么…编辑该问题以显示如何在视图中生成dropdownlist,以便我也可以更正。您能否确认生成的选项之一
@StephenMuecke丢弃的选项不一定存在。我只是ant知道所选值是“已丢弃”还是ID为“已丢弃”(下拉列表由tabState生成,丢弃可能是其中一个条目)
<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>