MVC.net,在partiaview中有一个表单,返回验证并在发布到Parent View后返回
本文关键字:返回 Parent View 验证 partiaview 有一个 表单 MVC net | 更新日期: 2023-09-27 18:19:59
我在局部视图中制作了一个sendemail表单,用于不同的视图,例如(联系人、铭文)
我的问题是我无法将错误消息或成功消息返回给父视图
我尝试不同的方法,我的两个更好的时间是:
-)在控件中使用Partialviewresult(显示消息,但在屏幕中只是部分视图,进行重定向)
[HttpPost]
public PartialViewResult _SendMail(MailModel obj)
{
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.ReplyToList.Add(new MailAddress(obj.from));
mail.To.Add(obj.to);
mail.From = new MailAddress(obj.from);
mail.Subject = obj.Subject;
string Body = obj.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
SendMailerController.sendMailer(mail);
ViewBag.courriel = "- votre courriel à été envoyé";
return PartialView();
}
else
{
ViewBag.courriel = "- votre courriel à été envoyé";
return PartialView();
}
}
将父视图的名称发送到部分视图以隐藏输入,并在控制器中使用带有重定向ToAaction的操作结果。。。显示良好的视图,但不显示消息。
主控制器
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
父视图CONTACT.cshtml
@{
ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
<section id="emailForm">
@Html.Partial("_SendMail", new ViewDataDictionary(ViewData) { { "adresse", "Contact" } })
</section>
部分视图_SendMail
@model IdentitySample.Models.MailModel
@{
ViewBag.Title = "Evoyez-nous un Courriel";
}
<h2>@ViewBag.Title</h2>
<fieldset>
@using (Html.BeginForm("_SendMail", "Home", FormMethod.Post, new
{
@class = "form-horizontal",
role = "form",
@id = "Formulaire"
}))
{
@Html.ValidationSummary("", new { @class = "text-error" })
<div class="text-success"> @ViewBag.courriel </div>
<table class="col-md-4">
<tr>
<td>To:</td>
<td>@Html.TextBoxFor(m => m.to)</td>
</tr>
<tr>
<td>Suject:</td>
<td>@Html.TextBoxFor(m => m.Subject)</td>
</tr>
<tr>
<td>Message</td>
<td>@Html.TextAreaFor(m => m.Body)</td>
</tr>
<tr>
<td>
<input type="hidden" name="adresse" value="@ViewData["adresse"]" />
@Html.TextBoxFor(m => m.from)
</td>
<td><input type="submit" value="Send" class="btn btn-default" /></td>
</tr>
</table>
<br/>
}
</fieldset>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
控制器发送电子邮件
[HttpPost]
public ActionResult _SendMail(MailModel obj)
{
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.ReplyToList.Add(new MailAddress(obj.from));
mail.To.Add(obj.to);
mail.From = new MailAddress(obj.from);
mail.Subject = obj.Subject;
string Body = obj.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
SendMailerController.sendMailer(mail);
ViewBag.courriel = "- votre courriel à été envoyé";
return RedirectToAction(obj.adresse, "Home");
}
else
{
ViewBag.courriel = "- votre courriel à été envoyé";
return RedirectToAction("Contact", "Home");
}
}
和emai型号
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;
namespace IdentitySample.Models
{
public class MailModel
{
public string from { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Courriel")]
public string to { get; set; }
[Required]
[StringLength(150, ErrorMessage = "Sujet trop long, max. 150 charactéres")]
[DataType(DataType.Text)]
[Display(Name = "Sujet")]
public string Subject { get; set; }
[Required]
[StringLength(3000, ErrorMessage = "le message est de max. 3000 charactéres")]
[DataType(DataType.Text)]
[Display(Name = "Sujet")]
public string Body { get; set; }
public string adresse { get; set; }
}
}
PD:发送电子邮件服务工作正常,显示错误或成功消息是我的问题
最后我用AjaxForm做的更容易。
下载jquery插件。
我对函数的局部视图和签名做了一些更改。
PartialView就绪
@model IdentitySample.Models.MailModel
@{
ViewBag.Title = "Evoyez-nous un Courriel";
}
<h3>@ViewBag.Title</h3>
<form method="post" class="form-horizontal" role="form" id="sendMail">
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger", @id="validation" })
<div class="text-success" id="success"> @ViewBag.courriel </div>
<div class="input-group row">
@Html.LabelFor(m => m.to, new { @class = "input-group-addon" })
@Html.TextBoxFor(m => m.to, new { @class = "form-control", @placeholder = "votrenom@courriel.com" })
</div>
<div class="input-group row">
<p class="input-group-addon">sujet </p>
@Html.TextBoxFor(m => m.Subject, new { @class = "form-control" })
</div>
<div class="input-group row">
@Html.LabelFor(m => m.Body, new { @class = "input-group-addon" })
@Html.TextAreaFor(m => m.Body, new { @class = "form-control", @rows = "8" })
</div>
<input type="hidden" name="adresse" value="@ViewData["adresse"]" />
<input type="submit" value="Envoyer" class="btn btn-success" />
</form>
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/form")
<script type="text/javascript">
$(function () {
$("#sendMail").ajaxForm({
url: "/Home/_SendMail?obj",
dataType: 'json',
success: processJson
});
function processJson(data) {
// 'data' is the json object returned from the server
$("#validation").html("");
$("#success").html(data);
}
});
</script>
控制器中的功能
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult _SendMail(MailModel obj)
{
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.ReplyToList.Add(new MailAddress(obj.to));
mail.To.Add(ConfigurationManager.AppSettings["mailAccount"]);
mail.From = new MailAddress(ConfigurationManager.AppSettings["mailAccount"]);
mail.To.Add(new MailAddress(obj.to));
mail.Subject = "site web - " + obj.Subject;
string Body = "Courriel de " + obj.to + " Contenu: " + obj.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
if (!SendMailerController.sendMailer(mail)) return Json("error", JsonRequestBehavior.AllowGet);
ViewBag.courriel = "- votre courriel à été envoyé";
return Json("success", JsonRequestBehavior.AllowGet);
}
return Json("error", JsonRequestBehavior.AllowGet);
}