フォームに単純な確認メール検証フィールドを書きたいのですが、どの方法が最適ですか? これまでの私のコードは、基本的な形式です。ConfirmationEmail フィールドについては、私が行ったように送信時だけでなく、電子メール フィールドのようなリアルタイムの検証が必要です。
コントローラ
public class ContactController : SurfaceController
{
/// <summary>
/// Renders the Contact Form
/// @Html.Action("RenderContactForm","ContactFormSurface");
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
//Return a partial view ContactForm.cshtml in /views/ContactFormSurface/ContactForm.cshtml
//With an empty/new ContactFormViewModel
return PartialView("Contact", new Contact());
}
[HttpPost]
public ActionResult Send(Contact Model)
{
//Check if the dat posted is valid (All required's & email set in email field)
if (!ModelState.IsValid)
{
//Not valid - so lets return the user back to the view with the data they entered still prepopulated
return CurrentUmbracoPage();
}
//Check if the dat posted is valid (All required's & email set in email field)
if (Model.Email != Model.ConfirmEmail)
{
//Not valid - so lets return the user back to the view with the data they entered still prepopulated
return CurrentUmbracoPage();
}
//Update success flag (in a TempData key)
TempData["IsSuccessful"] = true;
TempData["Name"] = Model.Name;
TempData["Email"] = Model.Email;
//All done - lets redirect to the current page & show our thanks/success message
return RedirectToCurrentUmbracoPage();
}
}
モデル
namespace CribisWeb.Models
{
public class Contact
{
[Required]
[DisplayName("Nome")]
public string Name { get; set; }
[Required]
[EmailAddress]
[DisplayName("Email")]
public string Email { get; set; }
[Required]
[EmailAddress]
[DisplayName("Conferma Email")]
public string ConfirmEmail { get; set; }
}
}
見る
@model CribisWeb.Models.Contact
@if (Convert.ToBoolean(TempData["IsSuccessful"]))
{
<h1>YAY!</h1>
<p>Thanks for sending your message, we will get back to you shortly.</p>
<p>
@TempData["Name"]
@TempData["Email"]
</p>
}
else
{
using (Html.BeginUmbracoForm<CribisWeb.Controllers.ContactController>("Send"))
{
@Html.ValidationSummary(true)
<div>
<br />
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
<br />
@Html.LabelFor(x => x.Email)
@Html.TextBoxFor(x => x.Email)
@Html.ValidationMessageFor(x => x.Email)
<br />
@Html.LabelFor(x => x.ConfirmEmail)
@Html.TextBoxFor(x => x.ConfirmEmail)
@Html.ValidationMessageFor(x => x.ConfirmEmail)
<br />
</div>
<input type="submit" value="Submit" class="btn-accept" />
}
}
ありがとう