Sorry, the code looks odd for me. First, add Required attribute to all your model fields, which are mandatory. You will not have to check the for collection in this case. ModelState will be automatically updated with state of the fields. Secondly, I don't understand
the using of FormCollection. You have the model - check properties like name and email from there. Cheers
Are you rebuilding the same view when the model is invalid?
Example:
[HttpPost]
public ActionResult AddressAndPayment(Order newOrder)
{
if (ModelState.IsValid)
{
newOrder.Username = User.Identity.Name;
newOrder.OrderDate = DateTime.Now;
storeDB.Orders.Add(newOrder);
storeDB.SaveChanges();
// Process the order
var cart = ShoppingCart.GetCart(this);
cart.CreateOrder(newOrder);
return RedirectToAction(“Complete”, new { id = newOrder.OrderId });
}
// Invalid -- redisplay with errors
return View(newOrder);
}
bullrout@gma...
Member
5 Points
49 Posts
Model level validation only works once?
Apr 03, 2012 08:43 AM|LINK
Hi There,
I'm trying to validate my model. The validation works once, the messages appear etc. When I try to submit the form again it does not validate.
What am I missing here?
Thanks in advance.
[HttpPost]
public ActionResult HandleFormSubmit([Bind(Prefix = "MyTestForm")] formModel model, FormCollection collection)
{
var value ="";
var value1 = "";
foreach (var key in Request.Form.AllKeys)
{
if (key.Equals("someModel.Name"))
{
if (Request.Form[key].Length <= 0)
{
ModelState.AddModelError(key, "Please provide a name");
}
}
if (key.Equals("someModel.Email"))
{
if (Request.Form[key].Length <= 0)
{
ModelState.AddModelError(key, "Please provide an email");
}
}
}
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
return RedirectToUmbracoPage(
new HiveId(
new Guid("71c84bd5d4014fd6b79fa01f00d09a65")));
}
mvc validation
vladnech
Member
175 Points
78 Posts
Re: Model level validation only works once?
Apr 03, 2012 09:29 AM|LINK
Bieters
Member
229 Points
48 Posts
Re: Model level validation only works once?
Apr 03, 2012 09:36 AM|LINK
I guess you see client side validation?
Are you rebuilding the same view when the model is invalid?
Example:
[HttpPost] public ActionResult AddressAndPayment(Order newOrder) { if (ModelState.IsValid) { newOrder.Username = User.Identity.Name; newOrder.OrderDate = DateTime.Now; storeDB.Orders.Add(newOrder); storeDB.SaveChanges(); // Process the order var cart = ShoppingCart.GetCart(this); cart.CreateOrder(newOrder); return RedirectToAction(“Complete”, new { id = newOrder.OrderId }); } // Invalid -- redisplay with errors return View(newOrder); }mvc validation
bullrout@gma...
Member
5 Points
49 Posts
Re: Model level validation only works once?
Apr 04, 2012 07:14 PM|LINK
Hi There,
I did try to add the required attribute to my fields. The model however is always valid. What else do I need to trigger this?
[Required(ErrorMessage = "Please provide a name")]
[StringLength(200, MinimumLength = 3)]
public virtual string Name { get; set; }
--the model state is always valid
if (!ModelState.isValid)
{
}
Young Yang -...
All-Star
21111 Points
1817 Posts
Microsoft
Re: Model level validation only works once?
Apr 05, 2012 08:29 AM|LINK
Hi
Please explain more. If you don't pass a Name to the Model, it always valid?
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
Bieters
Member
229 Points
48 Posts
Re: Model level validation only works once?
Apr 05, 2012 05:31 PM|LINK
Does model binding works correct? Expand the modelstate in debugger and look at values.