我按照本指南中有关如何在Umbraco中创建功能表单的所有步骤进行操作。 https://our.umbraco.org/documentation/getting-started/Code/Creating-Forms/
然后我继续添加一些额外的验证,通过使用[Required]和[EmailAddress]标记我的模型属性,并为每个输入添加验证消息到我的视图:
assert (MsgComp(cMsg, M1) && ... && MsgComp(cMsg, M72) && (cMsg == M1 || ... || cMsg == M72));
我的控制器设置为发送一封电子邮件,其中包含输入到输入字段的数据。目前它不起作用,因为我需要一个SMTP客户端:
@Html.ValidationMessageFor(m => m.Navn)
我对表单的看法使用了Umbraco表单助手方法:
[HttpPost]
public ActionResult Submit(ContactFormViewModel model)
{
if (!ModelState.IsValid) //validation
return CurrentUmbracoPage();
/// Work with form data here
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("thomb@hotmail.dk");
mailMessage.From = new MailAddress(model.Email);
mailMessage.Subject = "Kontakt mail";
mailMessage.Body = "Navn: " + model.Navn + "\nAddresse: " + model.Addresse + ", " +
model.PostNrBy + "\nTelefon: " + model.Telefon + "\n\n" + model.Message;
SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
smtpClient.Send(mailMessage);
return RedirectToCurrentUmbracoPage();
}
但是当我点击提交按钮时,不显示任何验证消息并重新加载页面。验证中是否有一个我缺失的步骤?
编辑:添加模型
@using (Html.BeginUmbracoForm("Submit", "ContactForm"))
{
<div class="col-sm-8">
<div class="row">
<div class="col-sm-4">
<form class="form-inline" id="kontaktForm">
<div class="form-group">
<label for="">Navn</label>
@Html.TextBoxFor(m => m.Navn, new { @class = "form-control", @placeholder = "Indtast navn..." })
@Html.ValidationMessageFor(m => m.Navn)
<label for="">Addresse</label>
@Html.TextBoxFor(m => m.Addresse, new { @class = "form-control", @placeholder = "Indtast addresse..." })
<label for="">Postnr. + by</label>
@Html.TextBoxFor(m => m.PostNrBy, new { @class = "form-control", @placeholder = "Indtast postnr & by..." })
<label for="">Telefon</label>
@Html.TextBoxFor(m => m.Telefon, new { @class = "form-control", @placeholder = "Indtast telefonnr..." })
<label for="">Email</label>
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "Indtast email..." })
@Html.ValidationMessageFor(m => m.Email)
</div>
</form>
</div>
<div class="col-sm-8">
<label for="">Skriv din besked</label>
@Html.TextAreaFor(m => m.Message, new { @class ="form-control", @id = "kontaktBesked", @rows = "11", @placeholder = "Indtast din besked her..."})
@Html.ValidationMessageFor(m => m.Message)
<button class="btn_1 pull-right contact-btn" name="submit" type="submit">Afsend Besked</button>
<button class="btn_1 contact-btn">Ryd felter</button>
</div>
</div>
</div>
}
答案 0 :(得分:1)
如果检查ModelState.IsValid并返回currentumbracopage(),则应显示错误消息。
否则您可以使用HTML5本机验证:
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "Indtast email...", @type="email" })
或使用setCustomValidation对输入的第一个元素进行JQuery和HTML5验证的组合。
EDIT为jquery添加了以下HTML5验证的代码
$('#v[element id]')[0].setCustomValidity("[validation error message]");
使其有效只需制作消息&#34;&#34;
$('#[element id]')[0].setCustomValidity("");