提问人:Aman Kadakuntla 提问时间:6/22/2022 更新时间:6/22/2022 访问量:176
如何在ajax json响应中将枚举int值转换为字符串值
How do I convert the enum int value to string value in ajax json response
问:
我有一个家庭控制器,它有一个对象列表和一个索引操作方法,它返回一个列出表中所有对象的视图。
Todo.cs
[Display(Name ="Item Id")]
[Required]
public int ItemId { get; set; }
[Display(Name ="Description")]
[Required(ErrorMessage ="Description is required")]
[StringLength(100,MinimumLength =10)]
[Remote("CheckForDescription","Home",ErrorMessage ="Description already exists")]
public string Description { get; set; }
[Display(Name = "Start Date")]
[Required(ErrorMessage ="Start date is required")]
public DateTime StartDate { get; set; }
[Display(Name = "End Date")]
[Required(ErrorMessage ="End date is required")]
public DateTime EndDate { get; set; }
[Display(Name = "Status of completion")]
public Status StatusOfCompletion { get; set; }
首页控制器.cs
public static List<Todo> ListOfTodos = new List<Todo>()
{
new Todo()
{
ItemId=101,
Description="Plan the module",
StartDate=DateTime.Now,
EndDate=DateTime.Now.AddDays(4),
StatusOfCompletion=Status.YetToStart},
new Todo()
{
ItemId=102,
Description="Dry run the plan",
StartDate=DateTime.Now.AddDays(3),
EndDate=DateTime.Now.AddDays(5),
StatusOfCompletion=Status.YetToStart
}
};
public ActionResult Index()
{
return View();
}
public JsonResult DisplayList()
{
return Json(ListOfTodos, JsonRequestBehavior.AllowGet);
}
索引.cshtml
@*@model IEnumerable<Todo_Application.Models.Todo>*@
@{
ViewBag.Title = "Index";
int count = 0;
}
<h2>List of todo items</h2>
<a href="/home/add" class="btn btn-primary">Add New Item</a>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Serial No</th>
<th>Item Id</th>
<th>Description</th>
<th>Start Date</th>
<th>End Date</th>
<th>Completion Status</th>
</tr>
</thead>
<tbody id="itemstable">
</tbody>
</table>
@section ajax{
<script>
$(document).ready(function () {
function convertToJavaScriptDate(value) {
var pattern = /Date\(([^)]+)\)/;
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}
$.ajax({
url: '@Url.Action("DisplayList","Home")',
success: function (data,status,req) {
for (i = 0; i < data.length; i++) {
alert(data[i].StatusOfCompletion);
str = "<tr><td>" + (i + 1) + "</td><td>" + data[i].ItemId + "</td><td>" + data[i].Description +
"</td><td>" + convertToJavaScriptDate(data[i].StartDate) + "</td><td>" + convertToJavaScriptDate(data[i].EndDate) +
"</td><td>" + data[i].StatusOfCompletion + "</td><td><a href=/home/edit/" + data[i].ItemId + ">Edit</a></td>";
$("#itemstable").append(str);
}
}
})
});
</script>
}
我正在使用 ajax 发送请求以填充表。 现在,问题是,StatusOfCompletion 是枚举,因此当我向 DisplayList 操作发送请求时,它会返回 json 数据。但是,在表中,枚举值显示为整数。我希望它显示为字符串值。我该怎么做?
答:
0赞
Ricardo Rodrigues
6/22/2022
#1
将此添加到“Todo.cs”中:
[JsonIgnore]
public int StatusOfCompletionInt
{
get { return (int)this.StatusOfCompletion; }
}
评论
0赞
Aman Kadakuntla
6/23/2022
我添加了它,但产生相同的输出。在控制器或视图中有什么要做的吗?
0赞
Ricardo Rodrigues
6/23/2022
在控制器或视图中,使用以下命令:Status statusStr = (Status)intValue;
评论