The user is able to post an outfit (a image/picture) to the website. If the user wants to post an outfit, he needs to give the outfit some values. The outfit needs to have a : Price, title, path (from file explorer) and a category (which is an enum).
The category can be chosen via a drop down menu, the title and price are given a value via a textbox.
So the conclusion is, to be able to post an outfit, you need to upload an image and give that image some values all in the same view. If one of the attributes isn't given a value (so for example no image is chosen, or the price is not given a value) there should be an error : one of the fields is missing.
When all attributes are given a value, the outfit with the given values goes to the database.
this is my outfit model:
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()
{
}
}
This is the controller so far:
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)
{
}
}
So inside the HttpPost method there should be some code which tells the program to give an error if one or more of the attributes I mentioned earlier is not given a value.
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)
Once every attribute of the outfit is given a value, then the outfit (image) and the values associated with it go to the database.
Thanks!
CodePudding user response:
"So inside the HttpPost method there should be some code wich tells the program to give an error if one or more of the attributes i mentioned earlier is not given a value?"
There are lot of way to do that. One is
Note:
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 yourcontroller action
argument will do that.Hope that would assist you accordingly what you are trying to implement.