Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I wanted to get your opinion on best practices for getting user input while doing something in a controller. I am inside a Action method in my controller and I am processing some data. The data may force me to ask the user for further input, based on that input I will then do something with that data.

What is the best practice to go about this?

1) How do I issue an alert or confirm box to get user input from the Controller? This Action is initiated by an update button on a Telerik MVC grid.

2) How do I persist my data from initially processing it, to asking the user for input, and then continue processing the data? It's a fair amount of data.

If the initial request was started from a ajax call in a javascript function, I think I would know how to do this. I would probably have the Action method pass back a JSON object back to the javascript method that would in turn ask the user and then decide which controller action to call and then pass this json object to it.

Am I on the right track here? If so, my problem would be with the Telerik MVC grid. I would need the update event of my grid to be bound client side.

Any help or thoughts on this is appreciated.

Steve

share|improve this question

migrated from stackoverflow.com Sep 20 '11 at 10:35

This question came from our site for professional and enthusiast programmers.

    
Since this is a question with a non-specific code answer (and is just asking for opinions) it doesn't belong here; it should be posted to Programmers.SE instead. –  George Stocker Sep 20 '11 at 1:12
    
I don't agree with you. The answer to this question does lead to some code samples. –  SteveB Sep 21 '11 at 6:50
add comment

2 Answers

up vote 1 down vote accepted

Why not intercept the OnSave event on the grid, client-side, and do your interaction there before allowing the client/server transaction to go forward? I'm not deeply familiar with the Telerik stuff, but it looks as if you can expand the data model to encompass whatever additional information you might need to send to the back end (beyond what's displayed in the grid), and you can modify this information in the event handler.

share|improve this answer
    
So I am going to try this. But, I think this might be a bit tricky. There is an onSave Client event that executes first before the server side update event on the grid. So I could do the first step there, in the client via Json, ask the necessary questions, then try figure out how to pass the data to the server which is automatically executed after the client event is done. This might not work if this is an ajax call, because the server side code might first start executing before the onSave event finishes. Thanks for the input. –  SteveB Sep 20 '11 at 5:30
    
SteveB: What I was meaning to suggest when I spoke about "expand[ing] the data model" is that you manage all of the out-of-band information through that data model, which (if possible) should obviate the need for any additional client-server transaction. If your OnSave action requires additional information up front, perhaps you could imbue the page with that information (in the form of JavaScript globals) when you generate the page. If the action needs to piggyback additional information on the Update transaction, it should do so through the data model (again, if possible). –  Jollymorphic Sep 20 '11 at 19:28
    
I'm not sure I quite understand you. I don't think it is a good user experience to ask the user a question that they may not need to answer most the time. I figure out while processing the data in the server side controller method, whether I need to ask additional questions. –  SteveB Sep 21 '11 at 6:48
add comment

You can't. HTTP is stateless; once you send a request from client browser to server, there is no "hey you didn't give me enough information! ALERTBOX!". Every interaction between your Controller and the client is done via Request/Response.

In other words, what you're saying in the latter half of your post is correct. You will need to design a server/client protocol that you can interface with in Javascript. Maybe you will just return an error code, with a message that your javascript can display in a box.

If you are trying to model a multi-step process, then you will probably need several Actions in your controller.

For example, you could model your process as a Controller.

public class CreateNewAccountProcessController:Controller{
   public JsonResult Step1(Step1Data data){
      // Persist Data Here (perhaps in session, or in data store)
   }
   public JsonResult Step2(Step2Data data){
      // Persist Data Here
   }
   public JsonResult Step3(Step3Data data){
      // Persist Data Here
   }
   public JsonResult Complete(CompleteData data){
   }
   public JsonResult Cancel(CancelData data){
      // Destroy/Nullify persisted data
   }
}

Then in your JS you could call

ajax('/createNewAccount/step1',formData);
ajax('/createNewAccount/step2',formData);
ajax('/createNewAccount/step3',formData);
ajax('/createNewAccount/complete',id);

What you could also do is track a process with a unique identifier so you can track a process.

share|improve this answer
1  
So that's what I thought, a multi-step process. So this now becomes a Telerik grid problem that I don't think I have an answer to. Unfortunately, the telerik grid automatically wires up the server side code. I will play around with the onSave client event to see if I can cancel the server side call. Thank you for the input. –  SteveB Sep 20 '11 at 5:34
    
Ah, good luck! I hate those Telerik controls :) –  Daryl Teo Sep 20 '11 at 5:36
    
So I can do this by cancelling the OnSave server-side controller action by using "e.preventDefault();" in the OnSave client event. So now I can do all the updating on client-side the with ajax calls, etc. HOWEVER: When I am done, how to I change the edited row back to it's display state, preserving any changes that were made? Steve –  SteveB Sep 20 '11 at 20:32
    
I'm not sure about the Telerik controls... but what I do is separate the form from the display -> if I cancel the editing, I just remove the form, and the display remains the same. –  Daryl Teo Sep 21 '11 at 0:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.