我一直在试图弄清楚这里有什么问题。我是c#的新手,对MVC来说还是比较新的。我的网站需要一个简单的小型电子邮件联系表格。我不完全确定押韵或原因是什么,但控制器将注册表单的所有字段,但跳过MethodArgumentNotValidException
并直接跳转到catch方法。我尝试在smtp.Send(msg);
之后添加[GET]
方法,但我仍然没有运气。我觉得这是smtp端口或主机的问题,但我找不到任何文档。
另外,我也尝试为按钮功能添加一些片段,但也没有运气。任何帮助都会很棒。
模型
[POST]
查看
public class ContactModels
{
[Required(ErrorMessage ="Your name is required")]
public string FullName { get; set; }
[Required(ErrorMessage = "Your email is required")]
public string Email { get; set; }
[Required(ErrorMessage = "Your phone number is required")]
public string Phone { get; set; }
[Required(ErrorMessage = "You must type a message")]
public string Comment { get; set; }
}
控制器
<div class="col-md-4">
<p><strong>Phone: </strong>586.xxx.xxxx</p>
<p><strong>Email: </strong><a href="mailto:Tom@blank.com">example@example.com</a></p>
<p><strong>Address: </strong>Address Here</p>
<h3>Connect with us through Email </h3>
@using (Html.BeginForm(FormMethod.Post))
{
@Html.ValidationSummary(true)
<div class="row">
@Html.LabelFor(model=>model.FullName, "Name: ")
@Html.EditorFor(model=>model.FullName)
@Html.ValidationMessageFor(model=>model.FullName)
</div>
<div class="row">
@Html.LabelFor(model=>model.Email, "Email: ")
@Html.EditorFor(model=>model.Email)
@Html.ValidationMessageFor(model=>model.Email)
</div>
<div class="row">
@Html.LabelFor(model=>model.Phone, "Phone: ")
@Html.EditorFor(model=>model.Phone)
@Html.ValidationMessageFor(model=>model.Phone)
</div>
<div class="row">
@Html.LabelFor(model=>model.Comment, "Message: ")
@Html.TextAreaFor(model=>model.Comment)
@Html.ValidationMessageFor(model=>model.Comment)
</div>
<div class="row">
<input type="submit" value="Send"/>
<input type="reset" value="Reset"/>
</div>
}
</div>
答案 0 :(得分:0)
试试这段代码:
catch(Exception ex)
{
return View("Error");
}