在 ActionResult 中驗證模型

[HttpPost]
public ActionResult ContactUs(ContactUsModel contactObject)
{
    // This line checks to see if the Model is Valid by verifying each Property in the Model meets the data validation rules
    if(ModelState.IsValid)
    {
    }
    return View(contactObject);
}

模型類

public class ContactUsModel
{
    [Required]
    public string Name { get; set; }
    [Required]
    [EmailAddress] // The value must be a valid email address
    public string Email { get; set; }
    [Required]
    [StringLength(500)] // Maximum length of message is 500 characters
    public string Message { get; set; }
}