using System.ComponentModel.DataAnnotations;
namespace DevAstuces.Models
{
public class Article
{
[Key]
public int Id { get; set; }
public virtual User Author { get; set; }
[Required(ErrorMessage = "Merci de spécifier une catégorie")] // This Error message is triggered
public virtual Category Category { get; set; }
[Required(ErrorMessage = "Merci de spécifier un titre")]
[Display(Name="Title", Prompt="Titre")]
public string Title { get; set; }
[Required(ErrorMessage = "Merci de spécifier une description")]
[Display(Name="Description", Prompt="Description")]
public string Description { get; set; }
[Required(ErrorMessage = "Merci de saisir un article")]
[Display(Name="Content", Prompt="Saisissez votre article")]
public string Content { get; set; }
public enum ArticleStatus
{
Pending,
Rejected,
Validated
}
public ArticleStatus Status { get; set; }
}
}
Category.cs
using System.ComponentModel.DataAnnotations;
namespace DevAstuces.Models
{
public class Category
{
[Key]
public int Id { get; set; }
public byte[] Image { get; set; }
[Required]
public string Name { get; set; }
}
}
ArticleViewModel.cs
using System.Collections.Generic;
using DevAstuces.Models;
namespace DevAstuces.ViewModels
{
public class ArticleViewModel
{
public List<Category> Category { get; set; }
public Article Article { get; set; }
}
}
ArticleController.cs
[HttpGet]
public IActionResult Publish()
{
var ArticleViewModel = new ArticleViewModel();
ArticleViewModel.Category = _articlecontext.Category.ToList();
return View(ArticleViewModel);
}
[HttpPost]
public IActionResult Publish(ArticleViewModel ArticleViewModel)
{
ArticleViewModel.Category = _articlecontext.Category.ToList();
if(!ModelState.IsValid)
return View(ArticleViewModel);
return RedirectToAction("Index","Home");
}
The Error may be linked to the fact that the ArticleViewModel uses a List of Category and that it is not directly linked to the Article which is to be created ?
Error in Article.cs :
[Required(ErrorMessage = "Merci de spécifier une catégorie")] // This Error message is triggered
public virtual Category Category { get; set; }
Category (List<Category> => user's choice is linked to Article.Category)
Title (Article.Title)
Description (Article.Description)
The only field that causes an "error" during ModelState Validation is Category. Even though the User does select a category, it acts like no categories were chosen.
[Required(ErrorMessage = "Merci de spécifier une catégorie")] // This error message is triggered
public virtual Category Category { get; set; }
According to your code exemple it should normally go through ModelValidation without any issue but no :
The only field that causes an "error" during ModelState Validation is Category. Even though the User does select a category, it acts like no categories were chosen.
Category is complex type but the view defines category as a string. You are not following model binding naming conventions or standard MVC programming patterns.
The code example in my last post specifically targets model binding and what a ViewModel should look like. The getting started tutorials in this site also illustrate these standard patterns and practices.
Thanks again for the time you take to help me, I'm really grateful.
Yet, I'm sorry but I don't understand what you mean by saying that I'm not following the model binding naming conventions.
I already read the tutorials you shared (several times) but according to what you say and what is in my Controller I don't see where I did a mistake ?
ArticleController.cs
[HttpGet]
public IActionResult Publish()
{
var ArticleViewModel = new ArticleViewModel();
ArticleViewModel.Category = _articlecontext.Category.ToList();
return View(ArticleViewModel);
}
[HttpPost]
public IActionResult Publish(ArticleViewModel ArticleViewModel)
{
ArticleViewModel.Category = _articlecontext.Category.ToList();
if(!ModelState.IsValid)
return View(ArticleViewModel);
return RedirectToAction("Index","Home");
}
Indeed I "populate" the Category list with a basic access to the Article DB and a LINQ query ToList()
Secondly, I use the following ViewModel which matches the official MVMC tutorial :
using System.Collections.Generic;
using DevAstuces.Models;
namespace DevAstuces.ViewModels
{
public class ArticleViewModel
{
public List<Category> Category { get; set; }
public Article Article { get; set; }
}
}
public class Article
{
public virtual Category Category { get; set; }
}
public class ArticleViewModel
{
public List<Category> Category { get; set; }
}
Even if you modify it to asp-for="@Model.Article.Category" now, the
Category in your Article is of the Category type, but when you use the
datalist, you get a string type. After submitting the form, even if the
name is the same, the string type will not be successfully bound to the
Category type.
Therefore, I suggest you modify the
Category in Article to string type.
Model
public class Article
{
[Required(ErrorMessage = "Merci de spécifier une catégorie")] // This Error message is triggered
public string CategoryName { get; set; }
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
11 Points
76 Posts
[MVC] Model validation fails with datalist
Feb 20, 2021 04:19 PM|valenciano8|LINK
Hi everyone,
I'm having an issue when I submit a HttpPost request on my Publish View through Model Validation.
Indeed, I get an Error Message for the ForeignKey Category on my Article Model.
I read the tutorial but couldn't find a solution : Model validation in ASP.NET Core MVC | Microsoft Docs
Here is the code :
Article.cs
Category.cs
ArticleViewModel.cs
ArticleController.cs
Publish.cshtml
The Error may be linked to the fact that the ArticleViewModel uses a List of Category and that it is not directly linked to the Article which is to be created ?
Error in Article.cs :
Best Regards
Member
11 Points
76 Posts
Re: [MVC] Model validation fails with datalist
Feb 21, 2021 10:45 AM|valenciano8|LINK
EDIT :
To be more precise, the issue is here :
I'd like to achieve something like :
This is why it's not working, how can I link an input with a datalist with ASP.NET Core ?
All-Star
53001 Points
23593 Posts
Re: [MVC] Model validation fails with datalist
Feb 21, 2021 01:26 PM|mgebhard|LINK
You have not clarified what "Not working" means. There's no datalist tag helper in Core if that's what you are asking.
Working example
Member
11 Points
76 Posts
Re: [MVC] Model validation fails with datalist
Feb 21, 2021 03:42 PM|valenciano8|LINK
Hi @mgebhard,
I mean, when the User fills those fields :
The only field that causes an "error" during ModelState Validation is Category. Even though the User does select a category, it acts like no categories were chosen.
According to your code exemple it should normally go through ModelValidation without any issue but no :
All-Star
53001 Points
23593 Posts
Re: [MVC] Model validation fails with datalist
Feb 21, 2021 05:37 PM|mgebhard|LINK
Category is complex type but the view defines category as a string. You are not following model binding naming conventions or standard MVC programming patterns.
The code example in my last post specifically targets model binding and what a ViewModel should look like. The getting started tutorials in this site also illustrate these standard patterns and practices.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-5.0&tabs=visual-studio
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0
You'll need to learn the basics before moving forward.
Member
11 Points
76 Posts
Re: [MVC] Model validation fails with datalist
Feb 21, 2021 08:18 PM|valenciano8|LINK
Hi @mgebhard,
Thanks again for the time you take to help me, I'm really grateful.
Yet, I'm sorry but I don't understand what you mean by saying that I'm not following the model binding naming conventions.
I already read the tutorials you shared (several times) but according to what you say and what is in my Controller I don't see where I did a mistake ?
ArticleController.cs
Indeed I "populate" the Category list with a basic access to the Article DB and a LINQ query ToList()
Secondly, I use the following ViewModel which matches the official MVMC tutorial :
Contributor
2690 Points
774 Posts
Re: [MVC] Model validation fails with datalist
Feb 22, 2021 07:15 AM|YihuiSun|LINK
Hi valenciano8,
<input list="Category" placeholder="Catégorie" name="Category">
<input list="Category" asp-for="@Model.Article.Category">
Model
public class Article { [Required(ErrorMessage = "Merci de spécifier une catégorie")] // This Error message is triggered public string CategoryName { get; set; } }
View
Best Regards,
YihuiSun
Member
11 Points
76 Posts
Re: [MVC] Model validation fails with datalist
Feb 22, 2021 08:52 AM|valenciano8|LINK
Hi @YiHuiSun,
Thanks a lot for your very detailed answer !
It works now, you were right !