gpt4 book ai didi

c# - 有条件地更改 [Display(Name = "")] 属性

转载 作者:太空宇宙 更新时间:2023-11-03 10:26:37 26 4
gpt4 key购买 nike

我有一个简单的要求,但无法理解如何实现。我的 View 模型中有这些属性。

public class Feedback
{
[Required]
[Display(Name = "Current ID")]
public int? PreviousID { get; set; }

[Required]
[Display(Name = "Next ID")]
public int? NextID { get; set; }

[Required]
public int ScenarioID { get; set; }

[Display(Name = "Select you scenario")]
public IEnumerable<SelectListItem> YourScenario { get; set; }
}

现在我的观点是,

<div class="form-group">
@Html.LabelFor(m => m.YourScenario, new { @class = "col-sm-3 control-label" })
<div class="col-sm-9">
@Html.DropDownListFor(m => m.ScenarioID, m.YourScenario, "Choose Scenario", new { @class = "form-control chosen-select" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(m => m.PreviousID, new { @class = "col-sm-3 control-label" })
<div class="col-sm-9">
@Html.TextBoxFor(m => m.PreviousID)
</div>
</div>

<div class="form-group" style="display:none">
@Html.LabelFor(m => m.NextID, new { @class = "col-sm-3 control-label" })
<div class="col-sm-9">
@Html.TextBoxFor(m => m.NextID)
</div>
</div>

在 View 中可以看到,只显示了 PreviousID 和 Scenario 的 DropDownList。使用 JQuery,我根据 DropDownList 的某些特定值显示和隐藏 NextID 的 div。因此,假设如果用户从下拉列表中选择“场景 3”,则会显示 NextID 的输入。

解释了所有这些之后,我想要的是根据下拉列表的选定值更改 PreviousID 的显示名称。因此,如果用户选择“场景 1”和“场景 2”,则 PreviousID 的显示名称应为“当前 ID”,如果用户选择“场景 3”,则显示名称应为“Previous ID”。此更改也应反射(reflect)在验证摘要中。那么请告诉我如何实现这一目标?非常感谢

最佳答案

我有两个想法:

1)

您可以删除行 [Display(Name = "Current ID")]。然后你需要使用 Label("SomeName") 而不是 LabelFor。然后创建一个事件,如 DropDownList_SelectionChanged,然后更改标签的名称。

2)

或者您添加一个成员 DisplayName 并绑定(bind)到它:

public class Feedback
{
[Required]
public int? PreviousID { get; set; }

[Required]
[Display(Name = "Next ID")]
public int? NextID { get; set; }

[Required]
public int ScenarioID { get; set; }

[Display(Name = "Select you scenario")]
public IEnumerable<SelectListItem> YourScenario { get; set; }

public String DisplayName {
get
{
String name = "";
if (ScenarioID == 1) {
name = "SomeName";
} else {
name = "otherName";
}

return name;
}
}
}

在你看来:

@Html.Label({Binding Content=m,Path=DisplayName}

这可能不完全正确,因为我无法测试您的项目,但我希望您能理解。请告诉我你尝试了什么。

关于c# - 有条件地更改 [Display(Name = "")] 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com