我正在进行一个以.Net 5
、为目标的Asp.Net core 5
项目
在视图中的许多情况下,我使用了select
列表,从列表中选择的值将影响到model
properties
中的一个。
我熟悉Action filters
,但这是我这个级别的初学者,所以我尝试创建一个自定义过滤器,检查model
中的任何property
的值是否等于default_option
。我会添加一个错误model state
,所有这些代码都应该在OnActionExecuting
方法中发生。
我尝试了什么:
public class CheckDefaultOptionFromSelect : ActionFilterAttribute
{
/// <inheritdoc />
public override void OnActionExecuting( ActionExecutingContext context )
{
var model = ( (Controller) context.Controller ).ViewData.Model;
if ( model is not null )
{
foreach ( var property in model.GetType().GetProperties() )
{
if ( (string) model.GetType().GetProperty( property.Name ).GetValue( model ).ToString() == "default_option" )
{
context.ModelState.AddModelError( "" , "Please select an option" );
base.OnActionExecuting( context );
break;
}
}
}
}
}
在这些操作中,视图将包含以下内容:
<div class="col-lg-4 col-sm-6">
<div class="form-group">
<div class="form-control-wrap">
<select asp-for="PropertyName" asp-items="@(new SelectList(myCollection,"Id","Title") )" class="form-select form-control form-control-lg form-control-outlined" data-ui="lg" data-search="on">
<option value="default_option">Default Option</option>
</select>
<label asp-for="PropertyName" class="form-label-outlined"></label>
<span asp-validation-for="PropertyName" class="text-danger"></span>
</div>
</div>
</div>
最重要的部分是:
<select asp-for="PropertyName" asp-items="@(new SelectList(myCollection,"Id","Title") )" class="form-select form-control form-control-lg form-control-outlined" data-ui="lg" data-search="on">
<option value="default_option">Default Option</option>
</select>
问题:当我使用break points
调试时,我发现( (Controller) context.Controller ).ViewData.Model
总是null
。
问题:如果不等于default_option
,我如何迭代所有model
属性并检查值?
您需要获得如下模型数据:
public class CheckDefaultOptionFromSelect : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
context.ActionArguments.TryGetValue("model", out object model);
if (model is not null)
{
foreach (var property in model.GetType().GetProperties())
{
if ((string)model.GetType().GetProperty(property.Name).GetValue(model).ToString() == "default_option")
{
context.ModelState.AddModelError("", "Please select an option");
base.OnActionExecuting(context);
break;
}
}
}
}
}
整个测试代码:
型号:
public class Test2
{
public int Id { get; set; }
public string Title { get; set; }
}
public class TestViewModel
{
public string PropertyName { get; set; }
}
视图:
注意:因为添加了带有空键的模型状态,所以asp-validation-for="PropertyName"
不会显示错误。请确保添加<div asp-validation-summary="ModelOnly" class="text-danger"></div>
以显示所有模型错误。
@model TestViewModel
@{
var myCollection = new List<Test2>()
{
new Test2(){ Id=1,Title="aaa"},
new Test2(){Id=2,Title="bbb"},
new Test2(){Id=3,Title="ccc"}
};
}
<form method="post" asp-action="Index">
<div class="col-lg-4 col-sm-6">
<div class="form-group">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-control-wrap">
<select asp-for="PropertyName" asp-items="@(new SelectList(myCollection,"Id","Title") )" class="form-select form-control form-control-lg form-control-outlined" data-ui="lg" data-search="on">
<option value="default_option">Default Option</option>
</select>
<label asp-for="PropertyName" class="form-label-outlined"></label>
<span asp-validation-for="PropertyName" class="text-danger"></span>
</div>
</div>
</div>
<input value="Create" type="submit" />
</form>
控制器:
public IActionResult Index()
{
return View();
}
[HttpPost]
[ServiceFilter(typeof(CheckDefaultOptionFromSelect))]
public IActionResult Index(TestViewModel model)
{
if (ModelState.IsValid)
{
return View();
}
else
{
return View(model);
}
}
在Startup.cs:中注册ActionFilterAttribute
services.AddScoped<CheckDefaultOptionFromSelect>();