Var*_*ois 1 validation asp.net-mvc-3 drop-down-menu
我有一个联系表单,将电子邮件发送到从dropDownList中选择的地址之一.但是当发布表单时,DropDownList始终无效.
完整型号:
public class ContactModels
{
[Display(Name = "Nome")]
[Required(ErrorMessage = "Nome é obrigatório.")]
[StringLength(100, ErrorMessage = "Nome não pode conter mais de 100 caracteres.")]
public string Name { get; set; }
[Display(Name = "Empresa")]
public string Company { get; set; }
[Display(Name = "E-mail")]
[Required(ErrorMessage = "Endereço de email é obrigatório.")]
[RegularExpression("[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Email não é válido.")]
public string Email { get; set; }
[Display(Name = "Telefone")]
[Required(ErrorMessage = "Telefone é obrigatório.")]
[StringLength(15, ErrorMessage = "Nome não pode conter mais de 15 caracteres.")]
public string Fone { get; set; }
[Display(Name = "Mensagem")]
[Required(ErrorMessage = "O texto do email é obrigatório.")]
[StringLength(500, ErrorMessage = "Nome não pode conter mais de 500 caracteres.")]
public string Message { get; set; }
[Display(Name = "Anexo")]
public string filePath { get; set; }
[Display(Name = "Departamento")]
public IEnumerable<SelectListItem> dropDownitems
{
get
{
return new[] {
new SelectListItem { Text = "Comercial", Value = "1"},
new SelectListItem { Text = "Financeiro", Value = "2"},
new SelectListItem { Text = "Parceria", Value = "3"},
new SelectListItem { Text = "RH/Curriculos", Value = "4"}
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
完整视图:
@model MvcAtakComBr.Models.ContactModels
@{
ViewBag.Title = "Contacts";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<div class="main_title">Contato</div>
@using (Html.BeginForm("Index", "contato", FormMethod.Post, new { enctype = "multipart/form-data" })) {
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Company)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Company)
@Html.ValidationMessageFor(model => model.Company)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Fone)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Fone)
@Html.ValidationMessageFor(model => model.Fone)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.dropDownitems)
</div>
<div class="editor-field">
@Html.DropDownList("dropDownitems")
@Html.ValidationMessageFor(model => model.dropDownitems)
</div>
<div class="editor-label">
arquivo:
</div>
<div class="editor-field">
<input type="file" name="filePath" id="filePath" />
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Message, new { cols = 35, rows = 5 })
@Html.ValidationMessageFor(model => model.Message)
</div>
<p>
<input type="submit" value="Enviar" />
</p>
}
Run Code Online (Sandbox Code Playgroud)
始终ModelState.IsValid为False.它不接受这个价值."值'address4@email.com'"无效".我尝试用数字或单个单词替换值,但没有成功.
Thanx提前
尝试使用强类型帮助器:
@Html.DropDownListFor(
x => x.SelectedItem,
new SelectList(Model.dropDownitems, "Value", "Text"),
"-- Select an item --"
)
Run Code Online (Sandbox Code Playgroud)
显然,您将SelectedItem
在视图模型上添加属性以保存所选值:
[Required]
public string SelectedItem { get; set; }
Run Code Online (Sandbox Code Playgroud)
就您在电子邮件字段中获得的验证错误消息而言,您可能需要对其进行调整.
归档时间: |
|
查看次数: |
4157 次 |
最近记录: |