Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation DropDownList值在MVC 3中始终无效_Validation_Asp.net Mvc 3_Drop Down Menu - Fatal编程技术网

Validation DropDownList值在MVC 3中始终无效

Validation DropDownList值在MVC 3中始终无效,validation,asp.net-mvc-3,drop-down-menu,Validation,Asp.net Mvc 3,Drop Down Menu,我有一个联系方式,可以将电子邮件发送到从下拉列表中选择的地址之一。 但是在发布表单时,DropDownList总是无效的 完整型号: public class ContactModels { [Display(Name = "Nome")] [Required(ErrorMessage = "Nome é obrigatório.")] [StringLength(100, ErrorMessage = "Nome não pode conter mais de 100

我有一个联系方式,可以将电子邮件发送到从下拉列表中选择的地址之一。 但是在发布表单时,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"}
            };
        }
    }
}
全景图:

@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>
}
Always ModelState.IsValid为False。它不接受这个值。价值'address4@email.com"是无效的。我尝试用数字或单个单词替换该值,但没有成功


Thanx Prevance

ContactModels是什么样子的?另外,您的全视图模型是什么?是否正确设置了其他值,或者只是下拉列表

粗略猜测,我会说这是控制器中的绑定错误。您需要在模型上指定前缀

[HttpPost]
public ActionResult Index([Bind(Prefix="MyViewModelName")]ContactModels contactForm)
{
    if (ModelState.IsValid)
    {
         //send the email
    }
}

ContactModels看起来像什么?另外,您的全视图模型是什么?是否正确设置了其他值,或者只是下拉列表

粗略猜测,我会说这是控制器中的绑定错误。您需要在模型上指定前缀

[HttpPost]
public ActionResult Index([Bind(Prefix="MyViewModelName")]ContactModels contactForm)
{
    if (ModelState.IsValid)
    {
         //send the email
    }
}

尝试使用强类型帮助器:

@Html.DropDownListFor(
    x => x.SelectedItem,  
    new SelectList(Model.dropDownitems, "Value", "Text"),
    "-- Select an item --"
)
其中,您将在视图模型上添加SelectedItem属性以保存选定值:

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

就电子邮件字段收到的验证错误消息而言,您可能需要对其进行调整。

尝试使用强类型帮助程序:

@Html.DropDownListFor(
    x => x.SelectedItem,  
    new SelectList(Model.dropDownitems, "Value", "Text"),
    "-- Select an item --"
)
其中,您将在视图模型上添加SelectedItem属性以保存选定值:

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

就您收到的电子邮件字段的验证错误消息而言,您可能需要对其进行调整。

您好,我有更多的值。所有其他值都可以正常工作。我已经用完整的模型和视图更新了我的问题。控制器:[HttpPost]public ActionResult IndexContactModels contactForm{如果ModelState.IsValid{您好,我有更多的值。所有其他值都可以正常工作。我已用完整的模型和视图更新了我的问题。控制器:[HttpPost]public ActionResult IndexContactModels contactForm{如果ModelState.IsValid{