在ASP.NET Core MVC中,ViewModel模型绑定不起作用可能是因为以下原因:
public class MyViewModel
{
public string Name { get; set; }
public int Age { get; set; }
// 其他属性...
}
asp-for
标签助手将输入字段与ViewModel的属性关联起来。
[HttpPost]
public IActionResult Submit(MyViewModel model)
{
if (ModelState.IsValid)
{
// 执行提交逻辑
return RedirectToAction("Success");
}
return View(model);
}
[BindProperty]
特性进行属性绑定。[BindProperty]
public string Name { get; set; }
[ModelBinder]
特性。public class MyModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
// 自定义处理逻辑
}
}
[ModelBinder(BinderType = typeof(MyModelBinder))]
public string Name { get; set; }
通过以上步骤,应该能够解决ASP.NET Core MVC中ViewModel模型绑定不起作用的问题。