I have a simple message system on my project. I have name property and if my User is in role Admin, I get the name by User.Identity.Name instead of getting it from View and since I made the name property required, ModelState is not being valid when an admin tries to post something.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Message,Name,Subject,Receiver")] Talep talep){
if (User.IsInRole("Admin")){
talep.Name = User.Identity.Name;
}
if (ModelState.IsValid){
talep.UploadDate = DateTime.Now;
var usrId = Request.Form["UserId"];
talep.Receiver = db.Users.Find(usrId).UserName;
talep.UserId = User.Identity.GetUserId().ToString();
db.Talep.Add(talep);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(talep);
}