你好我的问题是:我有两个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中。我怎么能这样做?