单击提交按钮后的ASP.Net MVC弹出消息

加里

我是MVC的新手,所以我不能完全确定是否已将模型绑定到控制器中并按原样查看。发生错误后,我试图弹出一条消息。在这种情况下,单击提交按钮后将发生错误。

风景...

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

    $(document).ready(function () {

        alert('@ViewBag.Message');

    });

</script>

}
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Guru Dental: Request Demo</title>

    <!-- CSS -->
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,400">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Droid+Sans">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lobster">
    <link rel="stylesheet" href="~/Content/assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="~/Content/assets/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="~/Content/assets/css/animate.css">
    <link rel="stylesheet" href="~/Content/assets/css/magnific-popup.css">
    <link rel="stylesheet" href="~/Content/assets/flexslider/flexslider.css">
    <link rel="stylesheet" href="~/Content/assets/css/form-elements.css">
    <link rel="stylesheet" href="~/Content/assets/css/style.css">
    <link rel="stylesheet" href="~/Content/assets/css/media-queries.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Favicon and touch icons -->
    <link rel="shortcut icon" href="~/images/favicon.ico" type="image/x-icon">
    <link rel="icon" href="~/images/favicon.ico" type="image/x-icon">

</head>

<body>

    <!-- Top menu -->
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#top-navbar-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <br>
                <a href="http://www.gurudental.com/"><img class="logo-size" src="~/images/guru-dental-slogan.png" alt=""></a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="top-navbar-1">
                <ul class="nav navbar-nav navbar-right">
                    <li><a href='@Url.Action("Index", "Home")'><br><br>Home</a></li>
                    <li>
                        <a href='@Url.Action("Contact", "Home")'><br><br>Contact</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- Page Title -->
    <div class="page-title-container">
        <div class="container">
            <div class="row">
                <div class="col-sm-12 wow fadeIn">
                    <h1>Demo Request</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Contact Us -->
    <div class="contact-us-container">
        <div class="container">
            <div class="row">
                <div class="col-sm-7 contact-form wow fadeInLeft">
                    @using (Html.BeginForm("DemoSubmit", "CRM", FormMethod.Post))
                    {
                        <h2>Rep Details</h2>
                        <div class="form-group">
                            <label for="contact-firstname">HSD Rep Code</label>
                            <input type="text" name="hsdrepcode" placeholder="Enter your HSD Rep code..." class="contact-name" id="contact-name">
                        </div>
                        <div class="form-group">
                            <label for="contact-lastname">Rep First Name</label>
                            <input type="text" name="hsfirstname" placeholder="Enter your Rep first name..." class="contact-name" id="contact-name">
                        </div>
                        <div class="form-group">
                            <label for="contact-lastname">Rep Last Name</label>
                            <input type="text" name="hslastname" placeholder="Enter your Rep last name..." class="contact-name" id="contact-name">
                        </div>
                        <h2>Doctor Details</h2>
                        <div class="form-group">
                            <label for="contact-currentlocation">Doctor's First Name</label>
                            <input type="text" name="firstname" placeholder="Enter your doctor's first name.." class="contact-subject" id="contact-subject">
                        </div>
                        <div class="form-group">
                            <label for="contact-currentlocation">Doctor's Last Name</label>
                            <input type="text" name="lastname" placeholder="Enter your doctor's last name..." class="contact-subject" id="contact-subject">
                        </div>
                        <div class="form-group">
                            <label for="contact-currentlocation">Doctor's Phone Number</label>
                            <input type="text" name="phonenumber" placeholder="Enter your doctor's phone number..." class="contact-subject" id="contact-subject">
                        </div>
                        <div class="form-group">
                            <label for="contact-currentlocation">Doctor's E-mail</label>
                            <input type="text" name="emailaddress" placeholder="Enter your doctor's e-mail..." class="contact-subject" id="contact-subject">
                        </div>
                        <button type="submit" class="btn">Submit</button>
                    }
                </div>

控制器...

public ActionResult RequestDemo()
{
    return View();
}

[HttpPost]
public ActionResult DemoSubmit(LeadInfo leadInfo)
{
    string salesEmail = CRMModels.GetNextSalesEmail();

    ViewBag.Message = CRMModels.AddLeadToCRM(leadInfo);

    if (ViewBag.Message == null)
    {
        EmailModels.SendEmailForLead(leadInfo, salesEmail);

        return RedirectToAction("Index", "Home");
    }
    else
        return RequestDemo();
    }
}

该模型...

if (hsdRepId != Guid.Empty)
{
    lead.Attributes["ree_hsdrepresentative"] = new EntityReference("ree_henryscheinrepresentative", hsdRepId);

    service.Create(lead);
}
else
{
    message = "Invalid HSD Representative Code";
}

return message;

在DemoSubmit函数中,如果有消息要显示,我需要返回视图以显示它。我怎么做?我尝试进行重定向,但这只是给了我一个新页面,其中没有输入任何数据,也没有显示消息。

谢谢,加里

用户名

您的代码有很多问题,特别是您没有将任何控件绑定到模型上,因此返回视图将始终显示空控件。您还应该在属性上包括验证属性,以防止发布和保存潜在的不良数据。您的视图还会生成无效的html(大量重复id属性)和<label>不是标签的标签(单击它们不会将焦点设置到关联的控件上,因为您没有将它们与控件关联。

根据您的观点,您的LeadInfo课程看起来像(建议的属性已添加)

public class LeadInfo
{
  [Display(Name = "HSD Rep code")]
  [Required(ErrorMessage = "Please enter your HSD Rep code")]
  public string hsdrepcode { get; set; }
  [Display(Name = "Rep first name")]
  [Required(ErrorMessage = "Please enter your Rep first name")]
  public string hsfirstname { get; set; }
  .....
  [Display(Name = "Doctor's e-mail")]
  [Required(ErrorMessage = "Please enter your doctor's e-mail")]
  [EmailAddress]
  public string emailaddress{ get; set; }   
}

您的GET方法应该是

public ActionResult RequestDemo()
{
  LeadInfo model = new LeadInfo();
  return View(model);
}

然后该视图应使用强类型的html helper绑定到您的属性并显示验证错误

@model LeadInfo
....
@using(Html.BeginForm())
{
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true) // this is where you custom error message will be displayed

  @Html.LabelFor(m => m.hsdrepcode) // displays the text associated with the DisplayAttribute and is associated with the following textbox
  @Html.TextBoxFor(m => m.hsdrepcode, new { placeholder="Enter your HSD Rep code...", @class="contact-name"})
  @Html.ValidationMessageFor(m => m.hsdrepcode)
  ....
  <button type="submit" class="btn">Submit</button>
}

您还应该包括以下脚本(最好使用@Scripts.Render()MVC以及MVC的捆绑和缩小功能)

jquery-{version}.js
jquery.validate.js
jquery.validate.unobtrusive.js

您还应该使用布局(母版页)

然后是post方法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RequestDemo(LeadInfo model) // not sure why you would change the name of the method?
{
  if(!ModelState.IsValid)
  {
    return View(model); // return the view to fix any validation errors
  }
  string errorMessage = CRMModels.AddLeadToCRM(leadInfo);
  if (errorMessage == null)
  {
    string salesEmail = CRMModels.GetNextSalesEmail();
    EmailModels.SendEmailForLead(leadInfo, salesEmail);
    return RedirectToAction("Index", "Home");
  }
  else
  {
    ModelState.AddModelError("", errorMessage);
    return View(model);
  }
}

尚不清楚您要返回的错误消息是什么,但是您的代码建议它可能与hsdrepcode属性相关联,在这种情况下,请修改代码以将错误与特定属性相关联

ModelState.AddModelError("hsdrepcode", errorMessage);

旁注:您的大多数代码都忽略了使用MVC的好处,以至于您可能不使用它。我建议您访问MVC网站并完成教程,

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

单击按钮并验证后如何调用使用ajax的asp.net MVC表单提交事件?

来自分类Dev

单击按钮后,单击“显示弹出窗口一段时间”,然后单击“重定向页面”-ASP.NET

来自分类Dev

如何检测在ASP.NET(MVC)中单击了哪个提交按钮

来自分类Dev

在MVC 4 ASP.net中提交表单后,保存dropdownlist状态和单选按钮状态

来自分类Dev

提交表单后在 mvc asp.net 中打开一个弹出窗口

来自分类Dev

ASP.NET MVC提交按钮本地化

来自分类Dev

ASP.NET MVC Core / 6:多个提交按钮

来自分类Dev

JavaScript中的下拉列表索引更改后,ASP.Net提交按钮单击事件不起作用

来自分类Dev

在 ASP.Net Core 中单击单选按钮提交表单

来自分类Dev

ASP.NET MVC淘汰按钮单击调用POST API

来自分类Dev

Asp.net MVC 5,单击按钮时显示标签?

来自分类Dev

成功后出现消息弹出窗口并禁用“提交”按钮

来自分类Dev

asp.net Ajax进度完成后显示弹出消息

来自分类Dev

ASP .NET在模态弹出窗口中的单击事件链接按钮上显示模态弹出窗口

来自分类Dev

单击按钮后,asp.net执行javascript函数

来自分类Dev

单击按钮后,ASP.NET GridView不出现

来自分类Dev

由模式弹出窗口提交的父页面中的ASP.NET MVC更新下拉列表

来自分类Dev

MVC创建按钮弹出需要帮助C#.net

来自分类Dev

ASP.NET MVC AJAX表单提交在提交/发布操作后丢失ListBoxFor / MultiSelectList

来自分类Dev

ASP.NET MVC中的jQuery模式弹出

来自分类Dev

ASP.Net Core MVC CRUD弹出模式

来自分类Dev

ASP .NET MVC Razor + JQuery UI delaog(登录弹出窗口)

来自分类Dev

在控制器中使用c#,MVC发送消息后如何弹出成功消息

来自分类Dev

用php提交表单后的弹出消息

来自分类Dev

如何在asp.net按钮单击上显示Bootstrap Autocloseable警报消息?

来自分类Dev

提交按钮后显示消息

来自分类Dev

在mailchimp中单击/提交按钮触发器后关闭弹出窗口

来自分类Dev

页面提交ASP.Net MVC时如何捕获选中哪个单选按钮

来自分类Dev

在ASP.Net Core MVC控制器中访问提交按钮的数据属性?

Related 相关文章

  1. 1

    单击按钮并验证后如何调用使用ajax的asp.net MVC表单提交事件?

  2. 2

    单击按钮后,单击“显示弹出窗口一段时间”,然后单击“重定向页面”-ASP.NET

  3. 3

    如何检测在ASP.NET(MVC)中单击了哪个提交按钮

  4. 4

    在MVC 4 ASP.net中提交表单后,保存dropdownlist状态和单选按钮状态

  5. 5

    提交表单后在 mvc asp.net 中打开一个弹出窗口

  6. 6

    ASP.NET MVC提交按钮本地化

  7. 7

    ASP.NET MVC Core / 6:多个提交按钮

  8. 8

    JavaScript中的下拉列表索引更改后,ASP.Net提交按钮单击事件不起作用

  9. 9

    在 ASP.Net Core 中单击单选按钮提交表单

  10. 10

    ASP.NET MVC淘汰按钮单击调用POST API

  11. 11

    Asp.net MVC 5,单击按钮时显示标签?

  12. 12

    成功后出现消息弹出窗口并禁用“提交”按钮

  13. 13

    asp.net Ajax进度完成后显示弹出消息

  14. 14

    ASP .NET在模态弹出窗口中的单击事件链接按钮上显示模态弹出窗口

  15. 15

    单击按钮后,asp.net执行javascript函数

  16. 16

    单击按钮后,ASP.NET GridView不出现

  17. 17

    由模式弹出窗口提交的父页面中的ASP.NET MVC更新下拉列表

  18. 18

    MVC创建按钮弹出需要帮助C#.net

  19. 19

    ASP.NET MVC AJAX表单提交在提交/发布操作后丢失ListBoxFor / MultiSelectList

  20. 20

    ASP.NET MVC中的jQuery模式弹出

  21. 21

    ASP.Net Core MVC CRUD弹出模式

  22. 22

    ASP .NET MVC Razor + JQuery UI delaog(登录弹出窗口)

  23. 23

    在控制器中使用c#,MVC发送消息后如何弹出成功消息

  24. 24

    用php提交表单后的弹出消息

  25. 25

    如何在asp.net按钮单击上显示Bootstrap Autocloseable警报消息?

  26. 26

    提交按钮后显示消息

  27. 27

    在mailchimp中单击/提交按钮触发器后关闭弹出窗口

  28. 28

    页面提交ASP.Net MVC时如何捕获选中哪个单选按钮

  29. 29

    在ASP.Net Core MVC控制器中访问提交按钮的数据属性?

热门标签

归档