- HTML code
@model MvcWithAjax.Models.SoSoKe_Ad@{ ViewBag.Title = "Index";}<script type="text/javascript" src="../../Scripts/jquery-1.6.2.js"></script><h2>Index</h2><script type="text/javascript"> $(function () { $("form").submit(function () { $.ajax({ url: $(this).attr("action"), type: "post", data: $(this).serialize(), success: function (data) { alert(data.msg); } }); }); })</script>@using (Html.BeginForm()) { <fieldset> <legend>广告</legend> <div>编号:@Html.TextBoxFor(model => model.Ad_ID)</div> <div>信息:@Html.TextBoxFor(model => model.Ad_Info)</div> <div> <input type="submit" value="提交" /> </div> </fieldset>}
- C# code
public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(SoSoKe_Ad ad) { return Json(new { msg = "1" }); } }
[HttpPost]
public ActionResult Index(SoSoKe_Ad ad) {
return Json(new { msg = "1" });
}
这个方法中实体类接收到的信息是正确的
可是返回的时候直接跳出个新的网页并没有alert 是什么情况?
求助啊~~~
------解决方案--------------------------------------------------------
public ActionResult Index(SoSoKe_Ad ad) 修改为 public ActionResult Index2(SoSoKe_Ad ad) 试试,方法名称不能相同
------解决方案--------------------------------------------------------
你这样是直接post而不是触发的ajax事件,mvc3里ajax提交表单用Ajax.BeginForm().
------解决方案--------------------------------------------------------
试一下这样
- C# code
@using (Ajax.BeginForm()) { <fieldset> <legend>广告</legend> <div>编号:@Html.TextBoxFor(model => model.Ad_ID)</div> <div>信息:@Html.TextBoxFor(model => model.Ad_Info)</div> <div> <input type="submit" value="提交" /> </div> </fieldset>}
------解决方案--------------------------------------------------------
- JScript code
$("form").submit(function () { AJAX操作... return false; });