一、异步的两种方法
- 用jQ的异步 返回content
- controllor:return Content(sum.ToString());//这里涉及到一个自动封装的问题
- html页:<form id="form1">
<input type="text" name="calc1" />+
<input type="text" name="calc2" />
<input type="button" id="btnAdd" value="加" />
<input type="text" name="sum" id="sum" />
</form> - $(function () {
$(‘#btnAdd‘).click(function () {
$.post(
‘@Url.Action("CalcAdd","Home")‘,//提交到的地址
$(‘#form1‘).serialize(),//把表单的数据序列化发送
function (msg) {//回调函数,msg是接收到的服务器端发来的数据
$(‘#sum‘).val(msg);
}
)
})
})
2.AjaxHelper类
- controller代码
- public ActionResult CalcAdd1(int calc1, int calc2)
- return Json(temp, JsonRequestBehavior.AllowGet);
- html代码
- @using (Ajax.BeginForm("CalcAdd1", "home", new AjaxOptions()
{
OnSuccess = "Success"
}))
{
<input type="text" name="calc1" />
<span>+</span>
<input type="text" name="calc2" />
<input type="submit" value="=" />
<input type="text" id="result" />
}
- @using (Ajax.BeginForm("CalcAdd1", "home", new AjaxOptions()
- 调用的脚本
- <script src="~/jquery-1.8.3.min.js"></script>
<script src="~/jquery.unobtrusive-ajax.min.js"></script>
- <script src="~/jquery-1.8.3.min.js"></script>
时间: 2024-12-13 06:52:25