How can I return a JSON result to a Ajax.BeginForm

后端 未结 6 1154
独厮守ぢ
独厮守ぢ 2021-02-02 08:49

I have got this simple form:

@using (Ajax.BeginForm(\"CreateProductFromAjaxForm\",\"Product\" , 
                  null, 
                  new AjaxOptions() {           


        
6条回答
  •  不知归路
    2021-02-02 09:33

    in page

    new AjaxOptions() {  
        OnSuccess = "getresult", 
    }
    

    in javascript

    function getresult(data){
       alert(data.x);
    }
    

    in c#

    [HttpPost]
    public ActionResult CreateProductFromAjaxForm(CreateProductModel model)
    {
        if (!ModelState.IsValid)
        {
            return Json("error", JsonRequestBehavior.AllowGet);
        }
    
       //add to database
    
        return Json(model, JsonRequestBehavior.AllowGet);
    } 
    

提交回复
热议问题