2013-08-24 122 views
0

数据注释我有这种情况:的联系表格

模型类:

public class ContatoModel 
{ 
    private string nome; 
    private string email; 
    private string telefone; 

    public ContatoModel() 
    { 
    } 

    public ContatoModel(string nome, string email, string telefone) 
    { 
     this.nome = nome; 
     this.email = email; 
     this.telefone = telefone; 
    } 

    public string Assunto { get; set; } 

    [Required(ErrorMessage = "Nome Obrigatório")] 
    public string Nome 
    { 
     get 
     { 
      return this.nome; 
     } 
     set 
     { 
      Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Nome" }); 
      nome = value; 
     } 
    } 
    [Required(ErrorMessage = "Email Obrigatório")] 
    public string Email { get; set; } 

    [Required(ErrorMessage = "Telefone Obrigatório")] 
    public string Telefone { get; set; } 

    public string Comentarios { get; set; } 
} 

的视角有:

 @using (Html.BeginForm("EnviarContato", "Contato")) 
     { 
      <div style="float: left; margin-bottom: 15px;"> 
       @Html.Label("Assunto")<br /> 
       @Html.DropDownList("ddlAssunto", assuntos, new {@class="dropdown"}) 
      </div> 
      <div style="clear: both"></div> 
      <div style="float: left; margin-bottom: 10px;"> 
       @Html.Label("Nome Completo", new { @class = "textfield" })<br /> 
       @Html.TextBox("txtNome", "", new { @class = "textfield" }) 
       @Html.ValidationMessage("Nome", "*") 
      </div> 
      <div style="clear: both"></div> 
      <div style="float: left; margin-bottom: 10px;"> 
       @Html.Label("Email")<br /> 
       @Html.TextBox("txtEmail", "", new { @class = "textfield" }) 
      </div> 
      <div style="clear: both"></div> 
      <div style="float: left; margin-bottom: 10px;"> 
       @Html.Label("Telefone")<br /> 
       @Html.TextBox("txtTelefone", "", new { @class = "textfield" }) 
      </div> 
      <div style="clear: both"></div> 
      <div style="float: left; margin-bottom: 10px;"> 
       @Html.Label("Comentários")<br /> 
       @Html.TextArea("txtComentarios") 
      </div>     
      <div style="clear: both"></div> 
      <div style="float: left; margin: 2px 20px 0px 255px;"> 
       @*<input type="image" src="/Content/images/button_send2.gif" />*@ 
       <input type="submit" value="Enviar" title="Enviar" /> 
      </div> 
     } 

而且在控制这个方法:

[HttpPost] 
    public ActionResult EnviarContato(FormCollection values) 
    { 
     try 
     { 
      string assunto = values["ddlAssunto"]; 
      string nome = values["txtNome"]; 
      string email = values["txtEmail"]; 
      string telefone = values["txtTelefone"]; 
      string comentarios = values["txtComentarios"]; 

      model.Assunto = assunto; 
      model.Nome = nome; 
      model.Email = email; 
      model.Telefone = telefone; 
      model.Comentarios = comentarios; 

      EnviarContato(); 

      return RedirectToAction("Index", new { envio = "OK" }); 
     } 
     catch (ValidationException ex) 
     { 
      throw new ValidationException(ex.Message); //return RedirectToAction("Index", new { envio = "NOK" }); 
     } 
    } 

我无法做数据注释客户端工作。 ValidationException发生在服务器端,但我想看到验证客户端消息,但它不起作用。 jquery文件仍然在我的母版页中加载。

另一个问题是我的视图有一个组合,由assunto变量加载,我不知道如何将它包含在验证中,以强制用户选择一个。

我的模型类不适用于数据实体。这只是接收表格值,验证,如果一切正常,发送电子邮件。

任何人都可以帮助我吗?

回答

0

我无法做数据注释客户端工作。 ValidationException发生在服务器端,但我想看到验证客户端消息,但它不起作用。 jquery文件仍然在我的母版页中加载。

  1. 的文件jquery.validate.jsjquery.validate.unobtrusive.js必须包含在页面中。
  2. <add key="UnobtrusiveJavaScriptEnabled" value="true" />必须在Web.Config中。

另一个问题是,我认为有一个组合,由可变ASSUNTO装了,我不知道如何将其纳入验证,强制用户选择一个。

其中一个空值为Options应该在您的DropDownList。例如“ - 选择一个 - ”。