【问题标题】:Force PascalCase when returning partial views with model from the mvc controller从 mvc 控制器返回带有模型的部分视图时强制 PascalCase
【发布时间】:2021-09-24 01:23:22
【问题描述】:

我有一个返回Partialview("_partialViewName", partialViewModel) 的操作方法。默认情况下,asp.net 核心使用的序列化程序会将属性名称转换为 camelCase,但我希望它使用 PascalCase。

public async Task<IActionResult> GetCustomer(int id)   
{
     var customer = GetCustomerFromDb(id);
     return PartialView("_CustomerPartialView", customer); 
}

响应返回到 JS 文件中的 ajax 方法的 success 函数。所有模型属性都在camelCase,但我需要它们在PascalCase

使用 JSON.Net(甚至尝试过 System.Text.Json)作为序列化程序,并且不想在 ConfigureServices() 中全局设置设置选项。它需要在控制器本身中。

这是我的设置配置:

var serializerSettings = new SerializerSettings 
{
     ContractResolver = new DefaultContractResolver()
} 

PartialView() 方法有 4 个重载,但没有一个接受 Json Setting 作为参数。

知道如何强制序列化程序使用 PascalCase 吗?谢谢

【问题讨论】:

    标签: c# serialization json.net asp.net-core-mvc system.text.json


    【解决方案1】:

    根据你的代码,当使用Ajax调用GetCustomer动作方法时,会返回一个部分视图给成功函数,该视图包含html元素和相关的属性值,相关的属性名是PascalCase, this screenshot,可以尝试使用F12开发者工具查看。

    我假设可能有一个动作/API 方法,它将返回一个对象列表。那么,当使用JQuery ajax调用这个方法时,响应的数据就是camelCase,像这样:

    在这种情况下,要让响应数据的属性名是PascalCase,可以在action方法中返回一个json字符串,然后在success函数中,得到json字符串后,可以使用JSON.parse()方法转换json字符串到Object,代码如下:

    API方法:

        [HttpGet("getAllStudent")]
        public string GetAllStudent()
        {
            var students = new List<StudentViewModel>()
            {
                new StudentViewModel(){ ID=101, Name="Tom", GPA="A"},
                new StudentViewModel(){ ID=102, Name="David", GPA="A"},
                new StudentViewModel(){ ID=103, Name="Jim", GPA="A"},
                new StudentViewModel(){ ID=104, Name="Vivian", GPA="A"},
                new StudentViewModel(){ ID=105, Name="Jack", GPA="A"},
            };
    
            //required System.Text.Json 
            var jsonString = JsonSerializer.Serialize(students);
            return jsonString;
        }
    

    JQuery Ajax 脚本:

            $.ajax({
                url: '/api/todo/getAllStudent',
                type: 'Get', 
                traditional: true,
                success: function (response) {
                    //use JSON.parse() method to convert the json string to object.
                    var result = JSON.parse(response);
                    //then, you can dynamically add the html elements and render the data.
                  
                },
                error: function (xhr, status) {
                    alert(status);
                }
            });
    

    结果如下:

    【讨论】:

    • 感谢您的回复。我宁愿让 asp.net 处理视图+模型并返回 html 而不是手动执行它。我希望他们有一个超载的PartialView() 接受 json 选项。我实际上通过为模型中的每个属性添加 JsonProperty 属性来解决我的问题。
    【解决方案2】:

    我最终使用JsonProperty 属性注释模型中的属性,因此序列化程序尊重模型中的帕斯卡大小写。

     public class Customer
     {
        [JsonProperty(nameof(Id))]
        public Guid Id {get; set;}
     } 
    

    显然这会覆盖 asp.net 中的驼峰式默认行为。

    【讨论】:

    • 你已经声明了。所有模型属性都在 camelCase 中。您的示例在 PascalCase 中。那么,哪一个是正确的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2011-04-06
    相关资源
    最近更新 更多