Hey guys. I am learning the webmatrix and i create a edit form which is almost like the tutorial "movie list" one.
and now i am trying to validate the form input using the Validation.For function(check the input before submit the page.)
but i found out that my [validation.for] function only works after the first submittion. which means i have to submit once then the validation will work to check the empty field automaticlly without another submittion. but before the first submittion, it
will not work. how did this happen? And also the check int function here didn't work but i don't know where was wrong.
here is my code, this is an edit page for a database list.
Thanks guys very much for your help!
@{Page.title="Edit Products";Layout="~/layout.cshtml";Validation.RequireField("product","You must enter a product name!");Validation.RequireField("price","Price is required");Validation.Add("price",Validator.Integer("Price must be an integer"));varproduct="";varprice="";varproductId="";if(!IsPost){if(!Request.QueryString["ID"].IsEmpty()){productId=Request.QueryString["ID"];vardb=Database.Open("product");vardbCommand="SELECT * FROM Productlist WHERE ID = @0";varrow=db.QuerySingle(dbCommand,productId);product=row.Product;price=row.Price.ToString();}else{Validation.AddFormError("No product was selected.");}}if(IsPost){if(Validation.IsValid()){product=Request.Form["product"];price=Request.Form["price"];productId=Request.Form["id"];vardb=Database.Open("product");varupdateCommand="UPDATE Productlist SET Product=@0, Price=@1 WHERE ID=@2";db.Execute(updateCommand,product,price,productId);Response.Redirect("~/Page");}}
}<!DOCTYPEhtml><html><head><metacharset="utf-8"/><title>Edit a Product</title><scriptsrc="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.js"></script><scriptsrc="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.js"></script><scriptsrc="~/Scripts/jquery.validate.unobtrusive.js"></script></head></head><body><h1>Edit a Product</h1>@Html.ValidationSummary()<formmethod="post"><fieldset><legend>Product Information</legend><div><labelfor="product">Edit Product Name:</label><inputtype="text"name="product"value="@product"@Validation.For("product")/>@Html.ValidationMessage("product")</div><div><p><labelfor="price">Edit Price:</label><inputtype="text"name="price"value="@price"@Validation.For("price")/></p>@Html.ValidationMessage("price")</div><inputtype="hidden"name="id"value="@productId"/>
The client-side Required validators fire when you attempt to submit the form. However, the data type checks do not work on the client. They only work on the server.
yukiyuqi
0 Points
1 Post
A "Validation.For" problem
Jul 17, 2012 10:04 AM|LINK
Hey guys. I am learning the webmatrix and i create a edit form which is almost like the tutorial "movie list" one.
and now i am trying to validate the form input using the Validation.For function(check the input before submit the page.)
but i found out that my [validation.for] function only works after the first submittion. which means i have to submit once then the validation will work to check the empty field automaticlly without another submittion. but before the first submittion, it will not work. how did this happen? And also the check int function here didn't work but i don't know where was wrong.
here is my code, this is an edit page for a database list.
Thanks guys very much for your help!
Mikesdotnett...
All-Star
154852 Points
19855 Posts
Moderator
MVP
Re: A "Validation.For" problem
Jul 17, 2012 12:36 PM|LINK
The client-side Required validators fire when you attempt to submit the form. However, the data type checks do not work on the client. They only work on the server.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: A "Validation.For" problem
Jul 19, 2012 04:56 AM|LINK
HI
You need Ajax.
For webmatrix you can use the jquery to implement.
WebMatrix, jQuery and Ajax: Together They Work like a Treat!
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Mikesdotnett...
All-Star
154852 Points
19855 Posts
Moderator
MVP
Re: A "Validation.For" problem
Jul 19, 2012 05:00 AM|LINK
"Need" AJAX? How does that resolve yukiyuqi's question? The article you linked to doesn't even look at Web Pages 2 or validation.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: A "Validation.For" problem
Jul 19, 2012 05:30 AM|LINK
Hi Mike
Thanks for warning me.
I will delete the answer.
Thanks.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework