Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do server-side validation using MVC 5

I am working on a simple auction website for a charity. I have an Item model for the sale items, and a Bid view where the user can enter a bid and submit it. This bid is received inside the Item controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Bid(int itemID, int bidAmount)
    {
        if (ModelState.IsValid)
        {
            Item item = db.Items.Find(itemID);
            if (bidAmount >= item.NextBid)
            {
                item.Bids++;
                item.CurrentBid = bidAmount;
                item.HighBidder = HttpContext.User.Identity.Name;
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {
                // Already outbid
            }
            return View(item);
        }
        return RedirectToAction("Auction");
    }

I would like to know how to display server-side validation to the user. For example, in the above code, it may be that the submitted bid amount is no longer sufficient. In that case, I would like to display a message to the user that they have been outbid etc.

How can I pass this information back to the view to display an appropriate message? I want the user to see the same item page view as before, updating the value in the edit box and displaying the message - similar to eBay. Thanks.

like image 212
Toby Sharp Avatar asked Feb 20 '14 14:02

Toby Sharp


People also ask

What is client-side validation and server side validation in MVC?

Validations can be performed on the server side or on the client side ( web browser). The user input validation take place on the Server Side during a post back session is called Server Side Validation and the user input validation take place on the Client Side (web browser) is called Client Side Validation.

How ModelState IsValid works in MVC?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .


1 Answers

you should have a look at the AddModelError Method of the ModelState Property.

   [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Bid(int itemID, int bidAmount)
    {
        if (ModelState.IsValid)
        {
            Item item = db.Items.Find(itemID);
            if (bidAmount >= item.NextBid)
            {
                item.Bids++;
                item.CurrentBid = bidAmount;
                item.HighBidder = HttpContext.User.Identity.Name;
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {
                // Already outbid
                ModelState.AddModelError("", "Already outbid");
            }
            return View(item);
        }
        return RedirectToAction("Auction");
    }

To Display the message in your view, you need a ValidationSummary

@Html.ValidationSummary(true)

like image 158
Andre Kraemer Avatar answered Sep 22 '22 13:09

Andre Kraemer