我想將值傳遞給我的控制器並執行查詢。然後,我想將查詢中的值返回給我的jquery函數,然後我可以將這些值分配給各種文本框。我無法弄清楚如何將數據返回給jquery。我只做了一個已經返回部分視圖的ajax調用。我正在ASP.NET中工作AJAX向控制器發送值並返回查詢結果
0
A
回答
0
你最好的選擇是使用Json。
創建一個C#模型服務器端:
var result = new ResultModel
{
Item1 = "This is a result",
Item2 = "Another thing to return",
Item3 = 5,
ItemArray = new List<string>{ "Thing 1", "Thing 2" }
};
return Json(result);
/* Server-side you don't *have* to use a model; an anonymous object is also fine, eg:
* return Json(new { Item1 = "This is a result" }); etc.
*/
然後你成功的Ajax功能將準備接受這個JSON結果:
$.post(url, data, function(json) {
$("#textBox1").val(json.Item1);
$("#textBox2").val(json.Item2);
// etc....
};
這裏假設你正在使用jQuery。其他框架有不同的客戶端語法。使用jQuery來做Ajax比自己編寫代碼要好得多。
jQuery通常可以計算出您是否發回json或html,但如果您得到奇怪的結果,您可能需要將$ .post替換爲$ .ajax,並指定您期待json作爲回報。
2
您可以使用Ajax調用的函數裏面像下面,你可以打電話,只要你需要這個功能..
function(id){
$.ajax({
url: "Your Controller/Method path",
data: JSON.stringify({
id: id
}),
dataType: "json",
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
if(data.success){
//Here you will get the value from the Controller if successfully executed
// you get values from data and you can assign those values to the textboxes based on your requirement..
}
}
})
}
控制器的方法:
public JsonResult functionName(int id)
{
JsonResult result = null;
try
{
var queryValue;
//here you can put your query and assign it to queryValue and return it back to the UI.
result = Json(new { success = true, data = queryValue }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
result = Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
}
return result;
}
}
希望這將幫助你..
相關問題
- 1. 顯示查詢結果存入發送郵件控制器
- 2. Ajax向控制器發送空數據
- 3. 向控制器發送Ajax調用
- 4. AJAX - 查詢搜索引擎並返回結果數
- 5. CodeIgniter/Ajax - 發送後值到控制器
- 6. PHP從控制器返回值到Ajax
- 7. 如何向服務器發送ajax調用而不期望返回結果?
- 8. 如何向網站發送查詢並解析結果?
- 9. 查詢返回無結果
- 10. PDO查詢返回結果
- 11. 代表發送回值並推送彈出視圖控制器
- 12. 從ajax查詢返回數據對象到控制器
- 13. 不顯眼的Ajax調用不發送查看模型返回到控制器
- 14. Solr 4返回0個查詢結果,但返回「*:*」查詢結果
- 15. Elasticsearch不返回聚合查詢結果,但返回其他查詢結果
- 16. 合併查詢返回值
- 17. 控制器AJAX方法返回AJAX ActionLink
- 18. ASP.NET MVC AJAX調用返回值從控制器查看
- 19. dropdownlist將null值返回給我的控制器操作結果
- 20. 發送JSON jQuery Ajax到PHP並返回
- 21. 限制Solr中過濾器查詢返回的結果數量
- 22. 返回查詢結果如果條件
- 23. 在Codeigniter中的控制器回顯/打印查詢結果
- 24. 修改列表中查看併發送回控制器
- 25. 查看結果,查詢返回與「puts」
- 26. ajax jquery不返回選擇查詢結果
- 27. 發送返回Ajax變量
- 28. jquery ajax返回html結果
- 29. 返回結果從ajax後
- 30. Ajax返回錯誤結果
你有什麼代碼嗎? – Ragnar 2014-10-28 13:58:09