.NET MVC5专题(控制器前后端交互的一些特性和注意事项)

/// 一个HttpGet 一次HttpPost
/// MVC怎么识别呢?不能依赖于参数识别(参数来源太多不稳定)
/// 必须通过HttpVerbs来识别,
/// 如果没有标记,那么就用方法名称来识别
/// [ChildActionOnly] 用来指定该Action不能被单独请求,只能是子请求
/// [Bind]指定只从前端接收哪些字段,其他的不要,防止数据的额外提交
/// [ValidateAntiForgeryToken] 防重复提交,在cookie里加上一个key,提交的时候先校验这个

[AcceptVerbs(HttpVerbs.Put | HttpVerbs.Post)] //[HttpPost]
//[HttpPost]
//[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ProductId, CategoryId, Title, Price, Url, ImageUrl")]JDCommodity commodity)
{
    string title1 = this.HttpContext.Request.Params["title"];
    string title2 = this.HttpContext.Request.QueryString["title"];
    string title3 = this.HttpContext.Request.Form["title"];
    if (ModelState.IsValid)//数据校验
    {
        JDCommodity newCommodity = this._iCommodityService.Insert(commodity);
        return RedirectToAction("Index");
    }
    else
    {
        throw new Exception("ModelState未通过检测");
    }
}
发布了147 篇原创文章 · 获赞 121 · 访问量 5114

猜你喜欢

转载自blog.csdn.net/weixin_41181778/article/details/103950278