JsonPropertyAttribute取消DisplayAttribute

时间:2015-08-17 20:02:44

标签: c# asp.net-mvc asp.net-mvc-4 c#-4.0 razor

我有一个模型类库,它定义了用于WebAPI中的JSON序列化以及ASP.NET MVC中的模型传输的DTO。因为它们是两个函数的模型,所以我的模型类用DisplayAttribute和JsonPropertyAttribute(Newtonsoft.Json)修饰,既可以提供人类可读的标签,也可以提供完全不同的序列化属性名称。我发现用两个属性修饰的类属性都会丢失其标签,而是显示属性名称(例如,“CompanyCode”而不是“Company Code”)。 DisplayAttribute和JsonPropertyAttribute之间是否存在已知的兼容性问题?也许是一个没有明确记录的优先顺序?

我真的不想在MVC项目中复制我的类只是为了将属性装饰简化为一个。我讨厌重复的代码。以下是该问题的一个示例:

public class Store {
    // This one doesn't work in Razor:
    [Display(Name = "Company Code")]
    [JsonProperty(PropertyName = "company_code")]
    public string CompanyCode { get; set; }

    // This one works in Razor without the JsonPropertyAttribute:
    [Display(Name = "Store Name")]
    public string Name { get; set; }
}

剃刀观点:

@model MyProject.Models.Store

<div>
    @Html.LabelFor(model => model.CompanyCode, htmlAttributes: new { @class = "control-label" })
    @Html.EditorFor(model => model.CompanyCode, new { htmlAttributes = new { @class = "form-control" } })
</div>

<div>
    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })
    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
</div>

WebAPI控制器方法:

public HttpResponseMessage GetStore(string companyCode) {
    return Request.CreateResponse(HttpStatusCode.OK, new MyProject.Models.Store {
        CompanyCode = companyCode,
        Name = "New Store"
    });
}

UPDATE 由于这两个属性的行为不一致以及我无法使其工作,我决定使用转换方法创建一个专用的ViewModel类。有关信息,这是新代码:

public sealed class Store {
    [JsonProperty(PropertyName = "company_code")]
    public string CompanyCode { get; set; }

    public string Name { get; set; }
}

public sealed class StoreModel {
    [DisplayName("Company Code")]
    public string CompanyCode { get; set; }

    [Required]
    public string Name { get; set; }

    public static StoreModel FromStore(Store store) {
        return new StoreModel {
            CompanyCode = store.CompanyCode,
            Name = store.Name
        };
    }

    public Store ToStore() {
        return new Store {
            CompanyCode = CompanyCode,
            Name = Name
        };
    }
}

1 个答案:

答案 0 :(得分:1)

请将属性Display更改为DisplayName:

[DisplayName("Company Code")]

我的完整代码:

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }

    [DisplayName("Last Name")]
    [JsonProperty(PropertyName = "last_name")]
    public string LastName { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var user = new User()
        {
            Id = 1, Name = "Julio", LastName = "Avellaneda"
        };

        return View("Index", user);
    }
}

public class UsersController : ApiController
{
    public HttpResponseMessage Get()
    {
        return Request.CreateResponse(HttpStatusCode.OK, new User
        {
            Id = 1, Name = "Julio", LastName = "Avellaneda"
        });
    }
}

如果我在fiddler中测试web api端点:

enter image description here

我的观点:

enter image description here 的问候,