哪个网站专做进口商品的,网站备案好麻烦,无锡网站制作哪家公司好,wordpress 在线商城在上一篇介绍MVC中的Ajax实现方法的时候#xff0c;曾经提到了除了使用Ajax HTML Helper方式来实现之外#xff0c;Jquery也是实现Ajax的另外一种方案。 通过get方法实现AJax请求 View script typetext/javascriptfunction GetTime() {$.get(Home/…在上一篇介绍MVC中的Ajax实现方法的时候曾经提到了除了使用Ajax HTML Helper方式来实现之外Jquery也是实现Ajax的另外一种方案。 通过get方法实现AJax请求 View script typetext/javascriptfunction GetTime() {$.get(Home/GetTime, function (response) {$(#myPnl).html(response);});return false;}
/script
div idmyPnl stylewidth: 300px; height: 30px; border: 1px dotted silver;
/div
a href# onclickreturn GetTime();Click Me/aController public ActionResult GetTime()
{return Content(DateTime.Now.ToString());
} 通过post方法实现Form的Ajax提交 View model MvcAjax.Models.UserModel
{ViewBag.Title AjaxForm;
}
script typetext/javascript$(document).ready(function () {$(form[action$SaveUser]).submit(function () {$.post($(this).attr(action), $(this).serialize(), function (response) {$(#myPnl).html(response);});return false;});});/script
div idmyPnl stylewidth: 300px; height: 30px; border: 1px dotted silver;
/div
using (Html.BeginForm(SaveUser, Home))
{tabletrtdHtml.LabelFor(m m.UserName)/tdtdHtml.TextBoxFor(m m.UserName)/td/trtrtdHtml.LabelFor(m m.Email)/tdtdHtml.TextBoxFor(m m.Email)/td/trtrtdHtml.LabelFor(m m.Desc)/tdtdHtml.TextBoxFor(m m.Desc)/td/trtrtd colspan2input typesubmit valueSubmit //td/tr/table
} Model using System.ComponentModel.DataAnnotations;namespace MvcAjax.Models
{public class UserModel{[Display(Name Username)]public string UserName { get; set; }[Display(Name Email)]public string Email { get; set; }[Display(Name Description)]public string Desc { get; set; }}
} Controller [HttpPost]
public ActionResult SaveUser(UserModel userModel)
{//Save User Code Here//......return Content(Save Complete!);
} 以上代码实现了Jquery POST提交数据的方法。 通过查看以上两种Jquery方式的Ajax实现方法和之前AJax HTML Helper比较可以发现其实Controller都是相同的。 采用Jquery方式提交数据的的主要实现方案就是通过Jquery的get或者post方法发送请求到MVC的controller中然后处理获取的response更新到页面中。 注意点 无论是使用超链接和form方式来提交请求javascript的方法始终都有一个返回值false用来防止超链接的跳转或者是form的实际提交。 这个地方就会出现一个疑问 如果针对该页面禁止了Javascript脚本那么该页面就是跳转或者是form就是提交返回的ActionResult处理就会出现问题了。 这个时候就需要在Controller中判断该请求是否是Ajax请求根据不同的情况返回不同的ActionResult [HttpPost]
public ActionResult SaveUser(UserModel userModel)
{//Save User Code Here//......if (Request.IsAjaxRequest())return Content(Save Complete!);elsereturn View();
} 转载于:https://www.cnblogs.com/MarchThree/p/3720408.html