HI
刚接触MVC,所以不是很清楚.特来请教一下.
控制器中有方法如下:
public string InitPassword(int id) { try { DataAccess.SystemUser systemUser = new DataAccess.SystemUser(); systemUser.InitPassword(id); return "初始化密码成功!"; } catch (Exception ex) { return ex.Message; } }
那么,如果,假如我要用@Ajax.ActionLink来创建一个<a>标签,从而进行Ajax请求,无论结果如何,我都像alert出来,那么,我该如何做?
谢谢.
得出结果时肯定要做判断,把返回值放在最外面的一层就好了
用Ajax.ActionLink该如何做呢?
Controller 中 Action:
1 [HttpPost] 2 public ActionResult AjaxTest(string id) 3 { 4 var message = string.Format("你的Id是: {0}", id); 5 6 return Json(new { Message = message }); 7 }
View 中 Ajax调用:
1 <a id="test" href="javascript:;">ajax</a> 2 3 <script type="text/javascript"> 4 $("#test").bind("click", function () { 5 $.ajax({ 6 type: "POST", 7 url: '/Home/AjaxTest', 8 dataType: 'json', 9 data: { 10 'id': '123456' 11 }, 12 success: function (r, status) { 13 alert(r.Message); 14 } 15 }); 16 }); 17 </script>
效果:
这个我懂
不能直接用Ajax.ActionLink达到这种效果吗?
同意楼上。