用ajax更新div,使用来自AJAX请求的内容更新DIV

你好我的问题是:我有两个div,一个div包含@ ajax.beginform,另一个div是空的但是隐藏了。我的想法是带有表单的div,要发出ajax请求,控制器的action方法检测模型是否有效,如果无效,重新发送相同的内容表单(带有表单内容的一个部分视图)但有验证错误; 如果模型有效,它会返回另一个部分视图内容,并带有一个简单的“谢谢”并显示在隐藏的div中,然后它应该隐藏具有该表单的div。我不知道怎么能这样做???我的想法是@ajax.beginform的onSuccess事件有参数,服务器返回的html,但不知道返回服务器的内容是什么,即我不知道div显示什么。请帮帮我。

PD:如果问题不明白,请告诉我,我会举一个例子。

[[[[[[UPDATE]]]]]]

我有一个模型:

public class Message{ public int MessageID { get; set; }

[Required]

public string From { get; set; }

[Required]

public string Content { get; set; }

}

public int MessageID { get; set; }

[Required]

public string From { get; set; }

[Required]

public string Content { get; set; }

}

我有控制器:

public class MessageController : Controller{

public ActionResult Index()

{

return View();

}

[HttpPost]

public ActionResult saveMessage(Message msg) {

// (ModelState.IsValid)

if (!msg.From.Contains("@")) {

ModelState.AddModelError("From", "The mail must contain an @");

return PartialView("DataMsg", msg);

}

return RedirectToAction("thanks");

}

public PartialViewResult thanks() {

return PartialView();

}

}

和我的观点:

Index.cshtml:

public ActionResult Index()

{

return View();

}

[HttpPost]

public ActionResult saveMessage(Message msg) {

// (ModelState.IsValid)

if (!msg.From.Contains("@")) {

ModelState.AddModelError("From", "The mail must contain an @");

return PartialView("DataMsg", msg);

}

return RedirectToAction("thanks");

}

public PartialViewResult thanks() {

return PartialView();

}

}

@model MvcApplication1.Models.Message

DataMsg.cshtml:

@{

ViewBag.Title = "Index";

}

Index

@section scripts{

function successMsg(data) {

// I want the "thank you" is displayed in the div with id="thanksUpdate" and not in #formUpdate

$("#formUpdate").html(data);

}

}

@using (Ajax.BeginForm("saveMessage",

new AjaxOptions { HttpMethod="Post", OnSuccess="successMsg"})) {

@Html.Partial("DataMsg", Model);

}

ViewBag.Title = "Index";

}

Index

@section scripts{

function successMsg(data) {

// I want the "thank you" is displayed in the div with id="thanksUpdate" and not in #formUpdate

$("#formUpdate").html(data);

}

}

@using (Ajax.BeginForm("saveMessage",

new AjaxOptions { HttpMethod="Post", OnSuccess="successMsg"})) {

@Html.Partial("DataMsg", Model);

}

和thanks.cshtml:

@model MvcApplication1.Models.Message

我希望“谢谢”显示在id =“thanksUpdate”的div中,而不是#formUpdate中。我怎么能这样做?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值