2016-11-25 27 views

回答

-1

用戶Jquery Ajax調用您的控制器的方法。

控制器代碼:

public class CustomerController : Controller 
{ 
    public ActionResult Index() 
    { 
    return View(); 
    } 

    [HttpPost] 
    public ActionResult UpdateOrder() 
    { 
    // some code 
    return Json(new { success = true, message = "Order updated successfully" }, JsonRequestBehavior.AllowGet); 
    } 
    } 

jQuery代碼:

$.ajax({ 
url: '@Url.Action("UpdateOrder")', // to get the right path to controller from TableRoutes of Asp.Net MVC 
dataType: "json", //to work with json format 
type: "POST", //to do a post request 
contentType: 'application/json; charset=utf-8', //define a contentType of your request 
cache: false, //avoid caching results 
data: {}, // here you can pass arguments to your request if you need 
success: function (data) { 
    // data is your result from controller 
    if (data.success) { 
     alert(data.message); 
    } 
}, 
error: function (xhr) { 
    alert('error'); 
} 
}); 

上面的代碼作爲參考從這裏開始:

http://www.c-sharpcorner.com/UploadFile/337dfd/useful-way-to-call-controller-actions-from-html-using-jquery/

jQuery to call Action Method in ASP.NET MVC C# by Ajax

希望這會有所幫助。

+1

儘管此鏈接可能回答問題,但最好在此處包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/14403228) –

+0

@VojtechRuzicka:感謝您指點我尋求更好的幫助。 –

+0

但我找不到在您的答案發送選中的值到控制器的任何地方@Naresh Parmar – user777

相關問題