基于字段值的自定义显示注释

时间:2013-01-22 16:22:53

标签: c# asp.net-mvc razor viewmodel

我有一个基于字段值的视图,必须显示自定义标签。

视图模型:

public class CreateAdViewModel
{
     public int Operation_Id { get; set; } 

     [Display(ResourceType = typeof(HeelpResources), Name = "AdViewModel_Price_Label")]
     public decimal Price { get; set; }
}

查看:

    <div id="price">
        @Html.DisplayNameFor(m => m.Price)
        @Html.TextBoxFor(m => m.Price) €
        @Html.ValidationMessageFor(m => m.Price)
    </div>

我想显示:

"Price: "  if Operation_Id = 1 (For the Sale of a Car)

并且

"Price Up To: "  if Operation_Id = 2 (For someone looking for a car until value XX €)

1 个答案:

答案 0 :(得分:0)

2个属性如何:

public class CreateAdViewModel
{
    public int Operation_Id { get; set; }

    [Display(ResourceType = typeof(HelpResources), Name = "AdViewModel_Price_Label")]
    public decimal? Price { get; set; }

    [Display(ResourceType = typeof(HelpResources), Name = "AdViewModel_MaxPrice_Label")]
    public decimal? MaxPrice { get; set; }
}

并在视图中:

<div id="price">
    @if (Operation_Id == 1)
    {
        @Html.DisplayNameFor(m => m.Price)
        @Html.TextBoxFor(m => m.Price) €
        @Html.ValidationMessageFor(m => m.Price)
    }
    else
    {
        @Html.DisplayNameFor(m => m.MaxPrice)
        @Html.TextBoxFor(m => m.MaxPrice) €
        @Html.ValidationMessageFor(m => m.MaxPrice)
    }
</div>
相关问题