DropDownList的值总是在MVC 3无效 [英] DropDownList Value is always invalid in MVC 3

查看:153
本文介绍了DropDownList的值总是在MVC 3无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接触的形式,即发送电​​子邮件从一个DropDownList选择的地址之一。
但是,当岗位的形式,将DropDownList始终是无效的。

I have one contact form, that send the email to one of Address selected from a dropDownList. But when post the form, the DropDownList is always invalid.

全模式:

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"}
            };
        }
    }
}

完整视图:

@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>
}

始终ModelState.IsValid为False。它不接受值。 价值address4@email.com'是无效的。我已经试过用数字或一个单词替换值,并没有成功。

Always ModelState.IsValid is False. it's not accepting the value. "Value 'address4@email.com'" is invalid". I've tried replace the value by number, or a single word and did not succeed.

感谢名单提前

推荐答案

尝试使用强类型的帮手:

Try using a strongly typed helper:

@Html.DropDownListFor(
    x => x.SelectedItem,  
    new SelectList(Model.dropDownitems, "Value", "Text"),
    "-- Select an item --"
)

在这里很明显,你会增加您的视图模型来保存选定值的SelectedItem 属性:

[Required]
public string SelectedItem { get; set; }

至于验证错误信息您收到的电子邮件领域涉及您可能需要调整。

As far as the validation error message you are getting for the Email field is concerned you might need to adjust it.

这篇关于DropDownList的值总是在MVC 3无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆