gpt4 book ai didi

asp.net-mvc - 单击提交按钮后 ASP.Net MVC 弹出消息

转载 作者:行者123 更新时间:2023-12-02 14:09:35 25 4
gpt4 key购买 nike

我是 MVC 新手,所以我不完全确定我是否已将模型绑定(bind)到 Controller 并按应有的方式查看。我试图在发生错误后弹出一条消息。在这种情况下,单击提交按钮后会发生错误。

景色...

@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>

Controller ...

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函数中,如果有消息要显示,我需要返回 View 来显示它。我怎么做?我尝试进行重定向,但这只是给了我一个新页面,没有输入任何数据,也没有显示消息。

谢谢,加里

最佳答案

在 Controller 上一旦出现错误,您可以将错误消息添加到 modalState

ModelState.AddModelError("", "Invalid HSD Representative Code");

要弹出错误警报,您可以使用 html 扩展方法来弹出消息

public static HtmlString PopAlert(this HtmlHelper htmlHelper, string alertType = "danger",
string heading = "")
{
if (htmlHelper.ViewData.ModelState.IsValid)
return new HtmlString(string.Empty);

var sb = new StringBuilder();

sb.AppendFormat("<div class=\"alert alert-{0} alert-block\">", alertType);
sb.Append("<button class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>");

if (!heading.IsNullOrWhiteSpace())
{
sb.AppendFormat("<h4 class=\"alert-heading\">{0}</h4>", heading);
}

sb.Append(htmlHelper.ValidationSummary());
sb.Append("</div>");

return new HtmlString(sb.ToString());
}

在 View 中,您只需像这样调用 Html 扩展方法

 @Html.PopAlert()

希望这对你有帮助:)

关于asp.net-mvc - 单击提交按钮后 ASP.Net MVC 弹出消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28881901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com