在回发后用MVC C#显示弹出确认信息 [英] Display popup confirmation message with MVC C# after postback

查看:498
本文介绍了在回发后用MVC C#显示弹出确认信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用具有C#编码的MVC框架。视图以标准HTML代码编写。
一旦用户点击提交按钮,我需要一条确认消息,您的消息已经发送

这是控制器:

  public ActionResult索引(ContactViewModel contactVM){
if(!ModelState.IsValid)
{
string url =请求.UrlReferrer.AbsolutePath +#contact;

return View();
}
其他
{
var contact = new联系人
{
名称= contactVM.Name,
Email = contactVM.Email,
Subject = contactVM.Subject,
Message = contactVM.Message
};
new Email()。发送(联系);
return RedirectToAction(Index);
}

这是查看:

 < input type =submitclass =submit_btn leftname =Submitid =submitvalue =Submit/> 
< input type =resetclass =submit_btn rightname =Resetid =resetvalue =Reset/>

请帮助。

解决方案

而不是 RedirectToAction(),返回查看

  new Email()。发送(联系); 

ViewBag.Message =发送消息;

return View();

在视图中:

  @if(ViewBag.Message!= null)
{
< script>

$(document).ready(function(){

alert('@ ViewBag.Message');

});

< / script>

}


Using MVC Framework with C# coding. The views are written in standard HTML code. I require a confirmation message saying "Your message has been sent" once the user clicks the submit button

Here is the controller:

public ActionResult Index(ContactViewModel contactVM){
        if (!ModelState.IsValid)
        {
            string url = Request.UrlReferrer.AbsolutePath+ "#contact";

            return View();
        }
        else
        {
            var contact = new Contact
            {
                Name = contactVM.Name,
                Email = contactVM.Email,
                Subject = contactVM.Subject,
                Message = contactVM.Message
            };
            new Email().Send(contact);
            return RedirectToAction("Index");
        }

Here is the View:

<input type="submit" class="submit_btn left" name="Submit" id="submit" value="Submit"/>
<input type="reset" class="submit_btn right" name="Reset" id="reset" value="Reset" />

Kindly assist.

解决方案

Instead of RedirectToAction(), return View :

    new Email().Send(contact);

    ViewBag.Message = "Message Sent"; 

    return View();

In View:

@if(ViewBag.Message != null)
{
<script>

$(document).ready(function(){

alert('@ViewBag.Message');

});

</script>

}

这篇关于在回发后用MVC C#显示弹出确认信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆