ASP.Net 6.0 Core MVC 中一个或多个字段未赋值时如何给出错误消息

How to give a error message when one or more fields are not given a value in ASP.Net 6.0 Core MVC

用户可以post一套衣服(image/picture)到网站。 如果用户想要 post 一件衣服,他需要给这件衣服一些值。服装需要有:价格标题路径(来自文件资源管理器)和类别(这是一个枚举)。

可以通过下拉菜单选择类别,通过文本框给标题和价格赋值。

所以结论是,为了能够 post 一套衣服,您需要上传一张图片并在同一视图中为该图片赋予一些值。如果其中一个属性没有被赋予一个值(例如没有选择图像,或者价格没有被赋予一个值)应该有一个错误: 缺少一个字段。

当所有属性都被赋予一个值时,具有给定值的服装将进入数据库。

这是我的服装型号:


public class OutfitVM
{
    public enum OutfitCategory
    {
        Trendy,
        Chic,
        Oldschool,
        Casual
    }

        [Required]
        public int? Prijs { get; set; }
        [Required]
        public string? Titel { get; set; }
        public string? FileAdress { get; set; }
        [Required]
        public OutfitCategory? DeCategory { get; }
        public bool Retry { get; set; }

        //public List<Review> Reviews { get; set; } = new List<Review>();

        public OutfitVM(string titel, int prijs, string fileadress, OutfitCategory 
        category)
    {
        this.Titel = titel;
        this.Prijs = prijs;
        this.FileAdress = fileadress;
        DeCategory = category;
    }
    
    public OutfitVM()
    {

    }
}

到目前为止,这是控制器:

  public class ToevoegController : Controller
        {
                private readonly ILogger<ToevoegController> _logger;
    
            public ToevoegController(ILogger<ToevoegController> logger)
            {
                _logger = logger;
            }
    
            public ActionResult OutfitToevoegen()  //IActionresult is een interface en 
                actionresult is een implimentatie daarvan
            {
                OutfitVM outfitVM = new OutfitVM();
                outfitVM.Retry = false;
                return View(outfitVM);
                //dit uitleg? wrm maak je nieuwe vm aan en wrm geef je die mee aan view
            }
    
            [HttpPost]
            public IActionResult OutfitToevoegen(OutfitVM outfit)
            {
                   
      
            }
        }

所以在 HttpPost 方法中应该有一些代码告诉程序如果我前面提到的一个或多个属性没有被赋予值则给出错误。

OutfitCategory = category (which is chosen via a drop down menu)
Prijs = price (which is given a value via a textbox)
Title = title (which is given a value via a textbox)
FileAdress = path (which is automatically given a value when the user chooses a picture from file explorer)

一旦服装的每个属性都被赋予了一个值,那么服装(图像)和与之关联的值就会进入数据库。

谢谢!

“所以在 HttpPost 方法内部应该有一些代码告诉程序如果我前面提到的一个或多个属性没有被赋予值则给出错误?”

There are lot of way to do that. One is model validation or model bindings. You can use [Required] as of your controller like public IActionResult OutfitToevoegen([Required] OutfitVM outfit)

控制器:

Model bindings using Required annotations can do the Model bindings on controller level

    [HttpPost]
    public IActionResult OutfitToevoegen([Required] OutfitVM outfit)
    {

        return Ok(outfit);
    }

示例模型:

public class OutfitVM
    {
        public string Prijs { get; set; }
        public string Titel { get; set; }
        public string FileAdress { get; set; }
    }

Output:

注:

If you check the captured screenshot, you would seen when I have sent empty request it responded with the 400 saying validation got failed as below:

So using [Required] annotation you can handle this without writing any extra validation code for that. [Required] as your controller action argument will do that.

希望这能相应地帮助您实现您想要实现的目标。