[英]MVC.net, with a form in partiaview, return validations and after post to Parent View
我在局部視圖中創建了一個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();
}
}
將父視圖的名稱發送到partialview以隱藏輸入,並在控件中使用帶有redirectToAaction ...的操作結果,顯示良好的View,但不顯示消息。
上級控制器
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.