如何使用MVC进行服务器端验证5 [英] How to do server-side validation using MVC 5

查看:146
本文介绍了如何使用MVC进行服务器端验证5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个简单的拍卖网站上为一个慈善机构工作。我有一个商品模型的销售项目和一个出价视图,用户可以输入出价并提交。这个出价是在Item控制器内收到的:

  [HttpPost] 
[ValidateAntiForgeryToken]
public ActionResult出价(int itemID,int bidAmount)
{
if(ModelState.IsValid)
{
项目项目= 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
{
//已经超出
}
return View(item);
}
return RedirectToAction(Auction);
}

我想知道如何向用户显示服务器端验证。例如,在上述代码中,可能是提交的投标金额不再足够。在这种情况下,我想向用户显示他们已经超出的消息等。



如何将此信息传回给该视图以显示适当的信息?我希望用户看到与之前相同的项目页面视图,更新编辑框中的值并显示消息 - 类似于eBay。谢谢。

解决方案

你应该看看ModelState属性的AddModelError方法。

  [HttpPost] 
[ValidateAntiForgeryToken]
public ActionResult Bid(int itemID,int bidAmount)
{
if ModelState.IsValid)
{
项目项目= 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
{
//已经超出
ModelState.AddModelError(,Already outbid);
}
return View(item);
}
return RedirectToAction(Auction);
}

要在视图中显示消息,您需要一个ValidationSummary



@ Html.ValidationSummary(true)


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.

解决方案

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)

这篇关于如何使用MVC进行服务器端验证5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆