英文:
ASP.NET 6 MVC Filter by enum passed into controller action from enum select list
问题
我在视图中有一个基于枚举的选择列表。我可以将选择列表中选择的值传递给控制器操作,但在筛选枚举值时遇到了困难。
换句话说,我有一个博客中的帖子列表,这些帖子具有"已准备就绪"的状态。我只想根据"已准备就绪"的状态筛选帖子。
问题是,帖子模型中的readyStatus
属性记录的是枚举的索引,而不是它的值。例如,枚举中的"Production Ready"值以"2"的值记录,这是它的索引。
因此,我已经尝试了一些选项。
1.) 从将传递到控制器操作的选择列表中获取索引值。
2.) 将选择列表的值(字符串),例如"ProductionReady",转换为枚举列表中的相应索引,然后根据该索引进行筛选。
在控制器操作的逻辑中,我尝试比较从选择列表传递的值。
任何帮助将不胜感激!
相关代码如下:
帖子模型
public ReadyStatus ReadyStatus { get; set; }
枚举
public enum ReadyStatus
{
Incomplete,
PreviewReady,
ProductionReady
}
控制器
public async Task<IActionResult> IndexFilter(string? readyStatus)
{
var posts = new List<Post>();
if (readyStatus is null)
{
posts = await _context.Posts
.Include(p => p.Blog)
.Include(p => p.BlogUser)
.OrderByDescending(p => p.Updated != null)
.ThenByDescending(p => p.Updated)
.ThenByDescending(p => p.Created)
.ToListAsync();
}
else
{
//int readyStatusInt = (int)ReadyStatus.$"{readyStatus}";
posts = await _context.Posts?
//.Where(p => p.ReadyStatus == readyStatusInt)
.Where(p => p.ReadyStatus.Equals(readyStatus))
.Include(p => p.Blog)
.Include(p => p.BlogUser)
.OrderByDescending(p => p.Updated != null)
.ThenByDescending(p => p.Updated)
.ThenByDescending(p => p.Created)
.ToListAsync();
}
ViewData["ReadyStatus"] = new SelectList(Enum.GetValues(typeof(ReadyStatus)).Cast<ReadyStatus>().ToList());
return View(posts);
}
视图
<form asp-action="IndexFilter" asp-controller="Posts">
<div class="input-group d-flex flex-md-row">
{{-- <label class="form-label me-2">Filter by Category:</label> --}}
<select name="readyStatus" asp-items="@ViewBag.ReadyStatus" class="form-select w-50"
onchange="this.form.submit()" onfocus="this.selectedIndex=-1;">
<option value="" selected disabled hidden>Filter By Status</option>
</select>
</div>
</form>
英文:
I have a select list in my view based on an enum. I can pass the value chosen in the select list to the controller action ok, but am having difficulty filtering on that enum value.
In other words. I have a list of posts in a blog that have a ready status. I just want to filter the posts by the ready status.
The thing is, the readyStatus property in the post model records the index of the enum, not its value. For example, the "Production Ready" value in the enum, is recorded with the value of "2", which is its index.
So I've been playing with a few options.
1.)Get the index value from the select list that will be passed into the controller action.
2.) Convert the select list value(string), for example "ProductionReady" and find its corresponding index in the enum list, then filter by that.
In the logic in the controller action, I am trying to compare the passed in value from the select list.
Any help would be greatly appreciated!
Relevant code below
Post model
public ReadyStatus ReadyStatus { get; set; }
Enum
public enum ReadyStatus
{
Incomplete,
PreviewReady,
ProductionReady
}
Controller
public async Task<IActionResult> IndexFilter(string? readyStatus)
{
var posts = new List<Post>();
if (readyStatus is null)
{
posts = await _context.Posts
.Include(p => p.Blog)
.Include(p => p.BlogUser)
.OrderByDescending(p => p.Updated != null)
.ThenByDescending(p => p.Updated)
.ThenByDescending(p => p.Created)
.ToListAsync();
}
else
{
//int readyStatusInt = (int)ReadyStatus.$"{readyStatus}";
posts = await _context.Posts?
//.Where(p => p.ReadyStatus == readyStatusInt)
.Where(p => p.ReadyStatus.Equals(readyStatus))
.Include(p => p.Blog)
.Include(p => p.BlogUser)
.OrderByDescending(p => p.Updated != null)
.ThenByDescending(p => p.Updated)
.ThenByDescending(p => p.Created)
.ToListAsync();
}
ViewData["ReadyStatus"] = new SelectList(Enum.GetValues(typeof(ReadyStatus)).Cast<ReadyStatus>().ToList());
return View(posts);
}
View
<form asp-action="IndexFilter" asp-controller="Posts">
<div class="input-group d-flex flex-md-row">
@*<label class="form-label me-2">Filter by Category:</label>*@
<select name="readyStatus" asp-items="@ViewBag.ReadyStatus" class="form-select w-50"
onchange="this.form.submit()" onfocus="this.selectedIndex=-1;">
<option value="" selected disabled hidden>Filter By Status</option>
</select>
</div>
</form>
答案1
得分: 1
你可以尝试以下方式将你收到的字符串转换为ReadyStatus:
ReadyStatus readyStatus = (ReadyStatus)Enum.Parse(typeof(ReadyStatus), "PreviewReady");
尝试:
.Where(x => x.ReadyStatus == ReadyStatus.PreviewReady)
现在在我的一侧可以正常工作。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论