0

I'm working on a website where I need to pass variables between actions. The variables are actually input fields. Here is some example markup.

<table>
    <tr>
        <td><input type="checkbox" name="major/1" value="1" checked /></td>
    </tr>
    <tr>
        <td><input type="radio" name="major/2" value="1" checked /></td>
        <td><input type="radio" name="major/2" value="2" /></td>
    </tr>
</table>

<a href="/NextPage/2/2">Next Page</a>

I basically want the values of those passed through a pagination system so it can be all used by the end. How could I accomplish this? Preferably without the use of a form surrounding the entire page.

2
  • 1
    What makes you think a form has to surround the entire page? It only needs to surround the elements you are submitting. (although in this case, it would have to surround the table in order to be valid html) Commented Apr 14, 2012 at 23:09
  • The table and pagination is literally the entire page minus the standard title and model at the top. Commented Apr 14, 2012 at 23:10

3 Answers 3

1

The only way to pass input form elements from one page to another is through a form post. You can certainly pass data via session or similar, but that's not passing them in form elements.

I don't understand you reluctance to use a form. hidden fields are form elements. They're used in forms. That's their purpose. You want to use form elements but not use a form? That's pretty pointless.

EDIT:

I think your problem is that you don't truly understand the way a browser and server work. They only communicate via get and post commands. Therefore, the only way to send an input to the server is via a post, and a post must contain a form. That's how a browser sends it's data.

There's a lot of things you could do via javascript, but all of those things would be a lot messier than just doing your post.

Sign up to request clarification or add additional context in comments.

1 Comment

I know how a browser and server work. I just wanted to know if there was a way to avoid wrapping the whole page in a form tag basically.
0

I could see two possible methods. First, use hidden input fields inside the postback form. This will ensure variables from the current iteration get saved to the model on postback:

@Html.HiddenFor(model=>model.Field1)

The second would be to save whatever you need to TempData on the controller side and retrieve it on the next postback:

TempData["tempkey"] = MyModel;

I think that whether the best solution is one of these (or something else) depends on what your model and controller logic looks like.

4 Comments

I'm assuming that means I have to wrap the whole page in a form?
No, but you do have an existing form on the page, right? Just add the hidden inputs that you want to save inside that form.
No, I do not have an existing form on the page.
I agree with Mystere Man's answer above... there's really no way but a form to pass data from multiple input fields back to the server. What's wrong with doing it that way, anyway?
0

The easiest way to accomplish this without using a form would be to use jQuery or javascript to grab the values and append them as url variables, assuming that is an acceptable approach. The other way would be to capture the values, send them ahead of time via ajax, and have your backend store them as session variables prior to following the link First, give the link a class name to listen to...

$('.navigation').on('click', '.pagelink', function(e){
    e.preventDefault();

    // url variables method
    var theUrl = ($(this).attr('href')) + '?';
    var ajaxUrl = $(this).attr('href');

    // needed for ajax method
    var theVals = new Object();
    $.each('table input', function() {
        // needed for ajax method
        var theName = $(this).attr('name');
        theVals[theName] = $(this).val();
        // url variables method
        theUrl = theUrl + theName + '=' + $(this).val() + '&';
    });
    // url variables method
    theUrl = theUrl.substr(0, theUrl.length - 2);// removes ending ampersand
    window.location = theUrl;

    // the ajax session method
    $.ajax({
        url : [MVC path to store session variables - ex: /ajax/setsession/],
        type : 'POST',
        data : theVals,
        success : function(data) {
            if(data) {
                // do something with data, like display success message
                window.location = ajaxUrl;
            }
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.