如何在提交表单后打开对话框显示张贴成功- asp.net MVC
How to open a dialog box after submit a form to show posted successfully - asp.net MVC
我正在做一个asp.net MVC项目。我想在表单发布后向用户显示一个对话框,让用户了解它是否成功,以及如果用户想要关闭对话框,这将是可能的。
我搜索了很多,读了一些东西,我可以返回部分视图,但我不确定部分视图是否是我在这种情况下需要的
我的问题是,是否有可能做到这一点与.dialog()
的java脚本的方式,或者如果我应该使用部分视图,那么请给我一点解释它是如何工作的。我需要这样的东西来显示后,用户单击提交按钮http://jsfiddle.net/db5SX/
这是我的控制器:
public ActionResult Index()
{
ViewBag.ResultMessage = TempData["ResultMessage"];
return View();
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Email,Phone,Message,Date")] Contact_US contact_US)
{
if (ModelState.IsValid)
{
db.Contact_US.Add(contact_US);
db.SaveChanges();
TempData["ResultMessage"] = "POSTED SUCESSFULLY...! We WILL CONTACT YOU SOON.";
return RedirectToAction("Index");
}
return View(contact_US);
}
如果需要,下面是我的观点:
<div class="form-group col-md-8">
<h3 class="txtformat padbot50px">Get in touch with us</h3>
<div class="text-danger message ">
@ViewBag.ResultMessage
</div>
@using (Html.BeginForm("Create", "Contact_Us", FormMethod.Post, new { @id = "contactusform" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group hidden">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12 contactusmsg">
@Html.TextAreaFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group contactuspostbtn">
<div class="col-md-12">
<input id="postcontactusmessage" type="submit" value="Send" class="btn btn-default" data-toggle="modal" data-target="#myModal" />
</div>
</div>
</div>
}
</div>
</div>
我的布局信息,如果需要:
<script src="~/Scripts/jquery-3.1.0.min.js"></script>
<link href="~/Scripts/jquery-ui-1.12.1.custom/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/tinymce/tinymce.min.js"></script>
这样做的一个好方法是将其抽象到一个基本控制器中,并在_layouts.cshtml
文件中显示消息,例如:
创建一个将被所有控制器继承的基本控制器
public class BaseController : Controller
{
public string SucccessMessage
{
get
{
return TempData["SuccessMessage"] as string;
}
set
{
TempData["SuccessMessage"] = value;
}
}
public string ErrorMessage
{
get
{
return TempData["ErrorMessage"] as string;
}
set
{
TempData["ErrorMessage"] = value;
}
}
}
在_Layouts 。cshtml,
<div class="container body-content" id="bodyContainer">
<div class="spacer"></div>
@if (@TempData["SuccessMessage"] != null)
{
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@TempData["SuccessMessage"]
</div>
}
else if (@TempData["ErrorMessage"] != null)
{
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@TempData["ErrorMessage"]
</div>
}
<div class="spacer"></div>
@RenderBody()
<hr id="hr" />
<footer>
<p>© @DateTime.Now.Year - Your APp </p>
</footer>
</div>
现在,你需要在你的控制器中做的就是:
public class MyController:BaseController
{
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Email,Phone,Message,Date")] Contact_US contact_US)
{
if (ModelState.IsValid)
{
db.Contact_US.Add(contact_US);
db.SaveChanges();
SucccessMessage = "POSTED SUCESSFULLY...! We WILL CONTACT YOU SOON.";
return RedirectToAction("Index");
}
return View(contact_US);
}
}
相关文章:
- 尽管链接成功并已成功下载,但未找到NPM模块
- 在另一个函数成功结束后调用该函数
- 在ajax成功后,cluetip不适用于首次点击活动元素
- jQuery成功函数中的ajax成功函数
- 刷新导致我的帖子“;张贴“;再一次
- jQuery触发器点击未从AJAX成功工作
- 如何使用jQuery可编辑插件检查ajax是否成功完成
- 执行ajax成功函数-jQUERY
- 解析云代码错误:解析.错误{代码:141,消息:“未调用成功/错误”}
- 如何从servlet获取数据到ajax成功
- 成功回调永远不会被JSONP请求调用
- 将表单张贴到MailChimp,但显示动态成功页面
- ajax请求成功,但可以'我看不到我的数据
- I'我试图在文本区域中进行特定的输入,调用特定的javascript函数,但没有成功;不起作用
- AngularJs|Don'在成功加载所有指令htmls之前,不显示页面
- Node.js Passport成功登录后显示用户名
- 通过mvc中的ajax动作链接获取关于成功的响应头
- 查询后websql成功回调无法访问变量
- AJAX调用在chrome扩展中未成功
- 如何在提交表单后打开对话框显示张贴成功- asp.net MVC