2011-07-06 30 views
1

我有一個聯繫表單,它將電子郵件發送到從dropDownList中選擇的一個地址。 但是,當發佈表單時,DropDownList始終無效。DropDownList值在MVC中始終無效3

全模式:

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是假。它不接受價值。 「價值‘[email protected]’」是無效的」。我試着用數字替換值,或一個字並提前

回答

6

嘗試使用強類型幫助程序:

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

很明顯,您會增加您的視圖模型的SelectedItem屬性來保存所選擇的值:

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

至於你所得到的電子郵件領域涉及您可能會在驗證錯誤消息需要調整它。

+0

這就像一個魅力..幹得好夥計和感謝名單。 – Varois

0

是什麼ContactModels看起來沒有成功。

感謝名單?另外,什麼是你的完整ViewModel?其他值是否設置正確,或者它只是下拉菜單?

在猜測,我會說你的控制器中有一個綁定錯誤,你需要指定

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

嗨,我確實擁有更多的價值。所有其他值工作正常。我用完整的模型和視圖更新了我的問題。 控制器: [HttpPost] 公衆的ActionResult指數(ContactModels聯繫形式) { 如果(ModelState.IsValid) { – Varois

相關問題