2012-04-27 5 views

答えて

1

コントローラのアクションメソッドを使用すると、Ajaxレスポンスが得られます。あなたは、あなたが使用してAJAXリクエストと通常のリクエストのためのHTMLマークアップを返すために同じアクション結果を使用することができます

public ActionResult GetItems() 
{ 
    var items=dbContext.Items; 
    return Json(items,JsonRequestBehavior.AllowGet); 
} 
public ActionResult GetAnother() 
{  
    return Content("I am just string from ajax"); 
} 

(...ビューを使用して、JSON文字列。画像など)HTMLマークアップのようなデータの任意の形式を返すことができますあなたのページからこれらのメソッドを呼び出すためにRequest.IsAjax方法

public ActionResul Getcustomer(int id) 
{ 
    var customer=dbcontext.Customer.Find(id); 
    if(Request.IsAjax()) 
    { 
    return View("PartialCustomerForm",customer); 
    } 
    return View("RegularCustomerForm",customer); 

} 

は、あなたがこれらのメンバーは、JavaScriptの端から呼び出された方法

$.get("@Url.Action("GetItems","Customer")", { id: 3 },function(data){ 
    //do whatever with the response. //alert(data); 
}); 
+0

jqueryのAJAXを使用することができますか?私はasmxで行ったのと同じように呼び出しますか? – Bonk

+0

@YonkShi:私の答えに追加されました。 – Shyju