I have created a control that I use often on my web sites. To this control I hava a ViewModel class that has a property witha required attribute. I my "master"page I show this partial view but i can not get the validation on the view right in my master page.
This is how I have done it in short.
I have a ascx page containing a textbox and a button. If the textbox i empty when I press the button i Would like to show the user a red box around the textbox and some validation test. The partial view is strongly type with an class that contains only a
string property for the textbox and has attribute [Required(Errormessage="Please enter a valid registration number")] just above the property.
Om my page that contains the partial view i looks very nice but I can not se the validation message or the red bos around the textbox how do I get the ModelState.IsValid from the partial view or how do I solve it so I can use the nice feature in DataAnnotations.
I ´have search over the net but can not find any example code how to solve it. Does it work without java script?
Validation messages requires the controls be inserted within a form, and the input filed must have associated a ValidationMessagefor helper.
If your partial view is inserted directly in the master page I imagine that you rendered it with Action or Render Action, in such a way you can associate a controller to your tectbox and button...otherwise requiring validation make no sense.
As Francesco said you must place the textbox and the submit button inside a form ( <% using (Html.BeginForm("Action", "Controller")) { %> //place textobox and button here <%} %> ).
When the property is invalid the textbox will have this css class applied: .input-validation-error
namespace Tacdis.DMS.Presentation.DMSWebApplication.Areas.PreSales.ViewModels
{
public class GetVehicleByRegNoViewModel
{
//Branch must be selected
[DisplayName("Registration number")]
[Required(ErrorMessage = "Please enter a valid reg. no")]
public string RegistrationNumber { get; set; }
}
}
As suggested by Radu the error is in the name correspondence between the ValidationMessageFor and The TextBox...they should have the same name passed.
To avoid name problem that might cause errors both in validation and in the model binding when the view is posted I suggest to ALWAYS USE
the ...For helpers that allows you only insert right names and insert for you the right prefix.....
TextBox, DropDown...etc are more useful for developers of helpers and controls than for View developers PLEASE AVOID USING THEM....to avoid wasting time with name errors.
Is there a way to see if the ViewModel is ok or not in the Controller writting something like if (!GetVehicleObject.RegistrationNumber.IsValid) return Error;
Yes I have tried this but my GetVehicelByRegNo is a different viewModel(user control) that the one I have the in the BusinessDeal.aspx page. So when i use
if(ModelState.IsValid){}
I get the required properties of the BusinessDealViewModel those have I checked before and dynamically added new user controls to the web page. Now I only want to check the SearchVehicle user control i have inserted in my page. I can see the error message
but I can not use the ModelState property.
What I want is to check if the state of my user control that is implemented in my master page is correct in a sipmple way.
I get the required properties of the BusinessDealViewModel those have I checked before and dynamically added new user controls to the web page.
Please show the action...
Honeybun76
Now I only want to check the SearchVehicle user control i have inserted in my page. I can see the error message but I can not use the ModelState property.
Does this post to the same action as above?
Please click 'Mark as Answer' if my reply has assisted you
Honeybun76
Member
25 Points
18 Posts
How do I use Validation on a partial control?
Feb 13, 2011 08:55 PM|LINK
Hi,
I have created a control that I use often on my web sites. To this control I hava a ViewModel class that has a property witha required attribute. I my "master"page I show this partial view but i can not get the validation on the view right in my master page.
This is how I have done it in short.
I have a ascx page containing a textbox and a button. If the textbox i empty when I press the button i Would like to show the user a red box around the textbox and some validation test. The partial view is strongly type with an class that contains only a string property for the textbox and has attribute [Required(Errormessage="Please enter a valid registration number")] just above the property.
Om my page that contains the partial view i looks very nice but I can not se the validation message or the red bos around the textbox how do I get the ModelState.IsValid from the partial view or how do I solve it so I can use the nice feature in DataAnnotations. I ´have search over the net but can not find any example code how to solve it. Does it work without java script?
Regards David
francesco ab...
All-Star
20954 Points
3286 Posts
Re: How do I use Validation on a partial control?
Feb 13, 2011 09:43 PM|LINK
Validation messages requires the controls be inserted within a form, and the input filed must have associated a ValidationMessagefor helper.
If your partial view is inserted directly in the master page I imagine that you rendered it with Action or Render Action, in such a way you can associate a controller to your tectbox and button...otherwise requiring validation make no sense.
Mvc Controls Toolkit | Data Moving Plug-in Videos
raduenuca
All-Star
24675 Points
4250 Posts
Re: How do I use Validation on a partial control?
Feb 14, 2011 02:27 AM|LINK
As Francesco said you must place the textbox and the submit button inside a form ( <% using (Html.BeginForm("Action", "Controller")) { %> //place textobox and button here <%} %> ).
When the property is invalid the textbox will have this css class applied: .input-validation-error
You can use this style:
.input-validation-error
{
border: 1px solid #f7951a;
background-color: #f9b665;
}
Radu Enuca | Blog
Honeybun76
Member
25 Points
18 Posts
Re: How do I use Validation on a partial control?
Feb 14, 2011 05:29 AM|LINK
Here is the code for the SearchVehicleUserControl.ascx
<% using (Html.BeginForm("GetVehicle", "BusinessDeal", FormMethod.Post)) { %> <div class="pairBlock inlineBlock" > <div style="height: 20px; width: 150px"> <%= Html.LabelFor(c => Model.RegistrationNumber)%>: </div> <div style="height: 20px; width: 150px"> <%=Html.TextBox("textRegistrationNumber", "", new { style = "width:100px; text-transform:uppercase" })%> <div> <%=Html.ValidationMessageFor(c=>c.RegistrationNumber) %> </div> </div> </div> <div class="inlineBlock" style="width:150px"> <input type="submit" id="buttonGetVehicle" value="Get Vehicle" /> </div> <div class="inlineBlock" style="width:120px;"> <%=Html.ActionLink("Search for vehicle", "FindVehicle", "Vehicle", new { area = "ProductAndServices" }, new { })%> </div> <%} %> <br /> <div class="greenLine" style="position: relative; top: 5px; width: 1100px;"> </div> </div>namespace Tacdis.DMS.Presentation.DMSWebApplication.Areas.PreSales.ViewModels { public class GetVehicleByRegNoViewModel { //Branch must be selected [DisplayName("Registration number")] [Required(ErrorMessage = "Please enter a valid reg. no")] public string RegistrationNumber { get; set; } } }<div style="display: block; vertical-align: top; width: 800px;"> <% if (!Model.HasSalesObject) Tacdis.DMS.DataTypes.PurchaseObject.Vehicle(); Model.Reg = new Tacdis.DMS.Presentation.DMSWebApplication.Areas.PreSales.ViewModels.GetVehicleByRegNoViewModel(); %> <div id="searchControl"> <% Html.RenderPartial("SearchVehicleUserControl", Model.Reg); %></div> </div>[Authenticated] [HttpPost] [HandleJsonException] public ActionResult GetVehicle(BusinessDealViewModel model, FormCollection collection, string submitButton) { SetModel(ActiveBusinessDealDataPool, ref model); if (!ModelState.IsValid) { return PartialView("SearchVehicleUserControl", model.Reg); }//Make the search for the vehicle... }raduenuca
All-Star
24675 Points
4250 Posts
Re: How do I use Validation on a partial control?
Feb 14, 2011 05:35 AM|LINK
replace with <%= Html.TextBoxFor(c=>c.RegistrationNumber, new { style = "width:100px; text-transform:uppercase" } ) %>
Radu Enuca | Blog
francesco ab...
All-Star
20954 Points
3286 Posts
Re: How do I use Validation on a partial control?
Feb 14, 2011 11:51 AM|LINK
As suggested by Radu the error is in the name correspondence between the ValidationMessageFor and The TextBox...they should have the same name passed.
To avoid name problem that might cause errors both in validation and in the model binding when the view is posted I suggest to ALWAYS USE
the ...For helpers that allows you only insert right names and insert for you the right prefix.....
TextBox, DropDown...etc are more useful for developers of helpers and controls than for View developers PLEASE AVOID USING THEM....to avoid wasting time with name errors.
Mvc Controls Toolkit | Data Moving Plug-in Videos
Honeybun76
Member
25 Points
18 Posts
Re: How do I use Validation on a partial control?
Feb 16, 2011 08:54 AM|LINK
Is there a way to see if the ViewModel is ok or not in the Controller writting something like if (!GetVehicleObject.RegistrationNumber.IsValid) return Error;
raduenuca
All-Star
24675 Points
4250 Posts
Re: How do I use Validation on a partial control?
Feb 16, 2011 09:03 AM|LINK
Like this:
if(ModelState.IsValid) { do something here }if you want to add an error to the ModelState (assuming you call an external service or other method to validate).
ModelState.AddError("","Error Message here"); //this will show in ValidationSummary
Radu Enuca | Blog
Honeybun76
Member
25 Points
18 Posts
Re: How do I use Validation on a partial control?
Feb 16, 2011 09:39 AM|LINK
Yes I have tried this but my GetVehicelByRegNo is a different viewModel(user control) that the one I have the in the BusinessDeal.aspx page. So when i use
if(ModelState.IsValid){}I get the required properties of the BusinessDealViewModel those have I checked before and dynamically added new user controls to the web page. Now I only want to check the SearchVehicle user control i have inserted in my page. I can see the error message but I can not use the ModelState property.
What I want is to check if the state of my user control that is implemented in my master page is correct in a sipmple way.
regards David
raduenuca
All-Star
24675 Points
4250 Posts
Re: How do I use Validation on a partial control?
Feb 16, 2011 09:46 AM|LINK
I don't think I understand.
Please show the action...
Does this post to the same action as above?
Radu Enuca | Blog