Does anyne know how to fix the following problem, when i use the code below and submit an empty textbox, my serverside validation pick up the error, but return the complate partial view, how can i get it to just return the error.
public ActionResult Locations(ValidationUserInput.ValidateWeatherSubmission VWS)
{
var wModel = VWS.strWeatherSearch;
if (ModelState.IsValid)
{
try
{
//var wModel = VWS.strWeatherSearch;
var SWR = IWR.GetWeatherCity(wModel);
int intLocationsFound = SWR.Count();
if (Request.IsAjaxRequest())
{
ViewData["Message"] = "Model State is Valid" + " " + wModel.ToString();
ViewData["WeatherLocationCount"] = (intLocationsFound == 1 ? "Found" + " " + SWR.Count() + " " + "location for: " + " " + wModel.ToString() : "Found" + " " + SWR.Count() + " " + "locations for: " + " " + wModel.ToString());
return PartialView("WeatherCitySearchResults", SWR.ToList());
}
else
{
return View("~/Views/Weather/NoJavaScriptEnabled.aspx", SWR);
}
}
catch (Exception)
{
///<comments>
///If an error occurs, display a friendly error message
///to the user informing them that the site cannot connect
///to the weather channnel at the moment.
///</comments>
return PartialView("UnableToContactWeather");
}
}
else
{
///<comments>
///If ModelState is not valid, return the user to the calling page
///and re-display the form with error message as to what needs amending.
///This check will also happen on the client side with javascript if enabled.
///</comments>
return PartialView("~/Views/Weather/DisplayWeatherCitySearchForm.ascx");
}
}
==================================
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Web.Domain.Models.Global.ValidationUserInput.ValidateWeatherSubmission>" %>
<%--<% Html.EnableClientValidation(); %>--%>
<%: ViewData["Message"] %>
<%: Html.TextBoxFor(m => m.strWeatherSearch)%><br />
<%: Html.ValidationMessageFor(m => m.strWeatherSearch)%>
<% %>
==================================
<% using (Ajax.BeginForm("Locations", "Weather", null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "dvWLR", LoadingElementId = "dvSearching", HttpMethod = "Post" }, new { @id = "frmCurrency", defaultbutton = "btnCurrency" }))
{ %>
<% Html.RenderPartial("~/Views/Weather/DisplayWeatherCitySearchForm.ascx"); %>
<input type="submit" id="btnCurrency" value="Enter Location" />
<% } %>
<div id="dvSearching">
spinning image will go here
</div>
<div id="dvWLR">
</div>
I've tried that and it still returns the full partial view. I'm beginning to think that putting a form in a partial view that is in a aspx page will only return the full partial view if a user inputs invald data. I have asked this question on numerous occasions,
posted it to other forums and also on Freelance websites and no one has an answer
Does anyne know how to fix the following problem, when i use the code below and submit an empty textbox, my serverside validation pick up the error,
but return the complate partial view, how can i get it to just return the error.
That's because you're returning the entire partial view when the ModelSate is not valid:
else
{
///<comments>
///If ModelState is not valid, return the user to the calling page
///and re-display the form with error message as to what needs amending.
///This check will also happen on the client side with javascript if enabled.
///</comments>
return PartialView("~/Views/Weather/DisplayWeatherCitySearchForm.ascx");
}
I have tried that, that, but i want the error message to appear below the text box(s), even with the example you have supplied i cannot do that, it display the error(s) in a list (my attempt anyway i was putting errors in a <ul><li>errors went here.</li></ul>.
So is what you are saying is this.
If catch errors on client, errors will appear under textbox, if catch errors on server, errors are displayed in new partial view?
I have tried that, that, but i want the error message to appear below the text box(s), even with the example you have supplied i cannot do that, it display the error(s) in a list (my attempt anyway i was putting errors in a <ul><li>errors went here.</li></ul>.
So is what you are saying is this.
If catch errors on client, errors will appear under textbox, if catch errors on server, errors are displayed in new partial view?
Regards
George
George,
No, the reason that you have different experiences depending on where the error is caught is due to your design.
If you want to display just the error messages, and have them appear in the ValidationMessageFor() elements you created, you need to return some javascript instead of a new partial view. In your example, you should return the following:
else
{
///<comments>
///If ModelState is not valid, return the user to the calling page
///and re-display the form with error message as to what needs amending.
///This check will also happen on the client side with javascript if enabled.
///</comments>
return "<script type=\"text/javascript\">$('#strWeatherSearch_validationMessage').html('" +
ModelState["strWeatherSearch"].Errors + "');</script>";
}
LearningASP_...
Member
242 Points
256 Posts
MVC3 Validation Problem
Mar 21, 2011 11:21 AM|LINK
Hi
Does anyne know how to fix the following problem, when i use the code below and submit an empty textbox, my serverside validation pick up the error, but return the complate partial view, how can i get it to just return the error.
public ActionResult Locations(ValidationUserInput.ValidateWeatherSubmission VWS) { var wModel = VWS.strWeatherSearch; if (ModelState.IsValid) { try { //var wModel = VWS.strWeatherSearch; var SWR = IWR.GetWeatherCity(wModel); int intLocationsFound = SWR.Count(); if (Request.IsAjaxRequest()) { ViewData["Message"] = "Model State is Valid" + " " + wModel.ToString(); ViewData["WeatherLocationCount"] = (intLocationsFound == 1 ? "Found" + " " + SWR.Count() + " " + "location for: " + " " + wModel.ToString() : "Found" + " " + SWR.Count() + " " + "locations for: " + " " + wModel.ToString()); return PartialView("WeatherCitySearchResults", SWR.ToList()); } else { return View("~/Views/Weather/NoJavaScriptEnabled.aspx", SWR); } } catch (Exception) { ///<comments> ///If an error occurs, display a friendly error message ///to the user informing them that the site cannot connect ///to the weather channnel at the moment. ///</comments> return PartialView("UnableToContactWeather"); } } else { ///<comments> ///If ModelState is not valid, return the user to the calling page ///and re-display the form with error message as to what needs amending. ///This check will also happen on the client side with javascript if enabled. ///</comments> return PartialView("~/Views/Weather/DisplayWeatherCitySearchForm.ascx"); } } ================================== <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Web.Domain.Models.Global.ValidationUserInput.ValidateWeatherSubmission>" %> <%--<% Html.EnableClientValidation(); %>--%> <%: ViewData["Message"] %> <%: Html.TextBoxFor(m => m.strWeatherSearch)%><br /> <%: Html.ValidationMessageFor(m => m.strWeatherSearch)%> <% %> ================================== <% using (Ajax.BeginForm("Locations", "Weather", null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "dvWLR", LoadingElementId = "dvSearching", HttpMethod = "Post" }, new { @id = "frmCurrency", defaultbutton = "btnCurrency" })) { %> <% Html.RenderPartial("~/Views/Weather/DisplayWeatherCitySearchForm.ascx"); %> <input type="submit" id="btnCurrency" value="Enter Location" /> <% } %> <div id="dvSearching"> spinning image will go here </div> <div id="dvWLR"> </div>====================================
Clip of error i'm getting
If No image please follow the link below
http://i52.tinypic.com/2ylpnp0.gif
Any help would be appreciated.
George
vipuldonga
Contributor
3753 Points
692 Posts
Re: MVC3 Validation Problem
Mar 21, 2011 12:00 PM|LINK
hi,
i think you have to put your text box in the Ajax.Beginform and then run and see the result.
Vipul Donga
Mark it as an answer, if it helped
if (MyAnswer)
MarkAsAnswer();
else
Repsonse.Write("correct me where i am wrong");
LearningASP_...
Member
242 Points
256 Posts
Re: MVC3 Validation Problem
Mar 21, 2011 12:30 PM|LINK
Hi Vipul Donga
I've tried that and it still returns the full partial view. I'm beginning to think that putting a form in a partial view that is in a aspx page will only return the full partial view if a user inputs invald data. I have asked this question on numerous occasions, posted it to other forums and also on Freelance websites and no one has an answer
Regards
George
Cullen Tseri...
Member
418 Points
85 Posts
Re: MVC3 Validation Problem
Mar 21, 2011 01:22 PM|LINK
That's because you're returning the entire partial view when the ModelSate is not valid:
else { ///<comments> ///If ModelState is not valid, return the user to the calling page ///and re-display the form with error message as to what needs amending. ///This check will also happen on the client side with javascript if enabled. ///</comments> return PartialView("~/Views/Weather/DisplayWeatherCitySearchForm.ascx"); }And then with the following code:
<% using (Ajax.BeginForm("Locations", "Weather", null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "dvWLR", LoadingElementId = "dvSearching", HttpMethod = "Post" }, new { @id = "frmCurrency", defaultbutton = "btnCurrency" })) { %> <div id="dvWLR"> </div>It updates the div tag "dvWLR".
If you want to return error messages in ajax, you could do the following:
In your action when the ModelState Is not Valid, get the errors out of the ModelState as follows:
if (!ModelState.IsValid) { return PartialView("ErrorMessage" ); }Where ErrorMessage partial view can display the ModelState.Values the way you want in HTML.
You could also get the errors from ModelState directly in the the partial view as follows:
LearningASP_...
Member
242 Points
256 Posts
Re: MVC3 Validation Problem
Mar 21, 2011 01:45 PM|LINK
Hi Cullen
I have tried that, that, but i want the error message to appear below the text box(s), even with the example you have supplied i cannot do that, it display the error(s) in a list (my attempt anyway i was putting errors in a <ul><li>errors went here.</li></ul>.
So is what you are saying is this.
If catch errors on client, errors will appear under textbox, if catch errors on server, errors are displayed in new partial view?
Regards
George
counsellorbe...
Member
540 Points
112 Posts
Re: MVC3 Validation Problem
Mar 21, 2011 02:19 PM|LINK
George,
No, the reason that you have different experiences depending on where the error is caught is due to your design.
If you want to display just the error messages, and have them appear in the ValidationMessageFor() elements you created, you need to return some javascript instead of a new partial view. In your example, you should return the following:
else { ///<comments> ///If ModelState is not valid, return the user to the calling page ///and re-display the form with error message as to what needs amending. ///This check will also happen on the client side with javascript if enabled. ///</comments> return "<script type=\"text/javascript\">$('#strWeatherSearch_validationMessage').html('" + ModelState["strWeatherSearch"].Errors + "');</script>"; }counsellorben