This is a migrated thread and some comments may be shown as answers.

PRG pattern when using Telerik Controls

5 Answers 77 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Karan Misra
Top achievements
Rank 1
Karan Misra asked on 13 Aug 2011, 10:47 AM
Hello,

So I understand from this thread that: So I understand from this thread that ModelState values (the AttemptedValues) are not used by Telerik Controls.

Is this a intended behavior? Because this is certainly different from the way the inbuild TextBoxFox, etc. work.

I am having a lot of issues try to implement PRG on a page with a Telerik ComboBoxFor.

When i simply render the View from the POST action, everything works as expected... however, when I save the ModelState (and restore, using TempData) and render the view from a GET action, the value is not retained.

What is the recommended solution for this?

Regards,
Karan Misra

5 Answers, 1 is accepted

Sort by
0
Stefan
Top achievements
Rank 1
answered on 18 Aug 2011, 05:17 PM
Wherever your link was pointed it got lost. :)

As far as I know ModelState-Values are only supposed to be prefered over Model-Values on POST-Requests. Some Telerik controls do not adhere to this, see here: http://www.telerik.com/community/forums/aspnet-mvc/numericinput/numerictextbox-modelstate.aspx

May I ask why you want to preserve the ModelState for a redirect anyways? This seems kind of odd to me.

The way I handle this is like this:
public ActionResult Index()
{
     return View("Index", GetGridData());
}
 
[HttpPost]
public ActionResult Create(Entity model)
{
    if(ModelState.IsValid)
    {
        try
        {
           DoStuff();
        }
        catch (Exception ex)
        {
            ModelState.AddModelError(string.Empty, ex.Message);
             
           return Index();
        }
 
        return RedirectToAction("Index", this.GridRouteValues());
    }
     
    return Index();
}

Above the Grid in the View I have a ValidationSummary which only displays non-property errors, so those don't get swallowed.
0
Karan Misra
Top achievements
Rank 1
answered on 19 Aug 2011, 04:49 AM
Hey Stefan!

Thanks for the reply.

If you look at the thread, I have replied their as well: http://www.telerik.com/community/forums/aspnet-mvc/numericinput/numerictextbox-modelstate.aspx

What you have done is not real PRG now is it? You have simply called the Index() method from within your [HttpPost] request. If validation fails you still render the View as a response to a post request which is against the PRG pattern (http://en.wikipedia.org/wiki/Post/Redirect/Get and for implementation detail on why I was trying to preserve the ModelState in the TempData: http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx#prg)

The fix should be Telerik modifying their controls to respect the ModelState over ViewData in all situations (just like the standard Html.* extensions.) This will allow us to implement PRG pattern.

Please correct me if I am wrong.
0
Stefan
Top achievements
Rank 1
answered on 19 Aug 2011, 12:16 PM
Form the wikipedia article: "When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original HTTP POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase."

That's why I only redirect the user on success, because that's where things could actually go down the drain if the user refreshes the page. Theres actually nothing bad that could happen if the user presses F5 after an invalid request, except if you manipulate data before the decision can be made if the request is valid or not. The worst thing that will happen is that the browser will ask the user if he wants to resubmit the (invalid) data and I will tell him again that the data is indeed still invalid.

I dont exactly know what will happen in your case if the user presses F5 after an invalid request, but since you are storing the ModelState in TempData and restoring it after the redirect, my best guess is that the browser will not interfere, but you will lose the modifications the user made to the form and he will have to start over. Unless you are preserving the ModelState after the redirect again.

Seems to add a lot of complexitiy for, at least in my case, no advantage at all.

But I can agree on one thing: Teleriks components should of course behave the same way as the rest of the MVC-Framework regarding the usage of ModelState. :)
0
Georgi Krustev
Telerik team
answered on 19 Aug 2011, 02:40 PM
Hello,

Currently, the ComboBox UI extensions does not behave correctly regarding the usage ModelState.
We will address this issue for the next release of Telerik Extensions for ASP.NET MVC!

Regards,
Georgi Krustev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Karan Misra
Top achievements
Rank 1
answered on 19 Aug 2011, 03:16 PM
Thanks a lot for the attention Georgi and Stefan. Glad we brought it to the attention of Telerik.

Waiting for the fix (and the much awaited internal build... which also has another bug fix which I had suggested.)
Tags
General Discussions
Asked by
Karan Misra
Top achievements
Rank 1
Answers by
Stefan
Top achievements
Rank 1
Karan Misra
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or