Chào mọi người em có tạo cái form bằng model thanhvien rồi trong class thanhvien em có thêm [Required(ErrorMessage =" không được để trống")] trước mỗi thuộc tính mà sao lúc em không nhập gì không thấy nó báo lỗi bên cạnh nhỉ?
Code form thanhvien razor
@using (Html.BeginForm("DangKyTV", "TaiKhoan", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TenTV, "Họ và tên", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.TenTV, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TenTV, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Username, "Tên đăng nhập", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, "Mật khẩu", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DiaChi, "Địa chỉ", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.DiaChi, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DiaChi, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DienThoai, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.DienThoai, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DienThoai, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ChucVu, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.ChucVu, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ChucVu, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Đăng ký" class="btn btn-default btn-success" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Quay lại trang chủ", "Index", "Home")
</div>
Code phần Controller
public ActionResult DangKyTV(tblThanhVien tv)
{
if (ModelState.IsValid)
{
db.tblThanhViens.Add(tv);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
else
{
return RedirectToAction("DangKy", "TaiKhoan");
}
}
Code phần tblThanhVien.cs
public partial class tblThanhVien
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblThanhVien()
{
this.tblHoaDons = new HashSet<tblHoaDon>();
}
public tblThanhVien(string name, string userName, string location, string email)
{
this.TenTV = name;
this.Username = userName;
this.DiaChi = location;
this.Email = email;
}
public int MaTV { get; set; }
[Required(ErrorMessage =" không được để trống")]
public string TenTV { get; set; }
[Required(ErrorMessage = "không được để trống")]
public string Username { get; set; }
[Required(ErrorMessage = "không được để trống")]
public string Password { get; set; }
[Required(ErrorMessage = "không được để trống")]
public string DiaChi { get; set; }
[Required(ErrorMessage = " không được để trống")]
public Nullable<int> DienThoai { get; set; }
[Required(ErrorMessage = " không được để trống")]
public string Email { get; set; }
public string ChucVu { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblHoaDon> tblHoaDons { get; set; }
}