Reputation: 9080
In my controller I have the following code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AdEntry adentry)
{
adentry.adDate = DateTime.Now;
adentry.adExpirationDate = DateTime.Now.AddDays(32);
adentry.adConfirmationID = rKeyGen(8);
if (ModelState.IsValid)
{
db.Items.Add(adentry);
db.SaveChanges();
TempData["Summary"] = adentry;
return RedirectToAction("Index");
}
return View(adentry);
}
In my Model I have this property:
[Required(ErrorMessage = "Confirmation Id is Required.")]
[StringLength(8, ErrorMessage = "{0} is too long.")]
public virtual String adConfirmationID { get; set; }
When I try to create a new Item ModelState.IsValid = false. The error I get is that Confiramtion Id is Required. I am setting the adConfirmationID = to a value right above the check. How can I get this check to pass?
Upvotes: 4
Views: 2625
Reputation: 73
There are Two ways to handle this issue one is you remove the updated/invalid field from ModelState by this
ModelState.Remove("foo");
or you have to pass the value to controller from View by using the hidden field
<input type="hidden" asp-for="foo"/>
Upvotes: 0
Reputation: 14302
Try this instead:
ModelState.Remove("adConfirmationID")
Place this code before checking ModelState.IsValid
This will fix your issue.
Upvotes: 5