0

In one of my views I got an INPUT field where I put my cursor and scan a MagCard through a reader. Which result in a series of chars in that box, ending with ENTER (char 13) which I catch via jQuery and post my form to another page to be processed.

Basicly I want the string from the input box to be available in a controller, so I can deal with it using c# functions instead of doing it all client side via javascript.

Getting values to managed code from client code seems to have eluded all guides I have found so far.

Any help appreciated, and the reason for me to POST and not GET is because its filled with special chars, ?'s and spaces which need to be preserved.

Any method will be valued.

@Darin Dimitrov Sorry for me being unclear.

While your suggestion works, and guided me a little towards what I need. (This will be handy to know no matter what.)

As I don't really want a return value, I was trying to post to Home/ProcessScan as action for my form. Then in my controller make this:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ProcessScan(string scanSeek)
    {
        string myScan = scanSeek;
        string name = myScan.Substring(1, 13);
        return Redirect("../reservation?name=" + name);
    }

And this now works for me. Had to:
* Use the [AcceptVerbs(HttpVerbs.Post)] declaration
* Change my type to ActionResult, instead of void, since redirect need return in front of it

Note: for my redirect I use "../" because this view is not under Home.

2 Answers 2

1

Your question is confusing as you are not providing much details. Obviously events like onfocus and onclick can only be handled through client side javascript. You mention jquery and POST so I suppose that you are talking about AJAX. So you could send a value like this:

$.post('@Url.Action("Foo")', { value: $('#mytextboxid').val() }, function(result) {
    // the value was successfully posted to the server
});

and on the server you would have the following controller action:

public ActionResult Foo(string value)
{
    // TODO: do something with the value and return a corresponding result
}
Sign up to request clarification or add additional context in comments.

Comments

1

Let's say that the input id is magCardCode so that you can have an Action that takes an argument: string magCardCode.

In this scenario you could simply post your form using jquery ajax like this:

<script>
    $(document).ready(function () {
        $("#submitForm").click(function () {
            $.ajax({
                url: '/MagCard/SubmitCode/,
                dataType: 'html',
                type: 'POST',
                success: function (data) {
                    // Use data
            });
        });

    });
</script>

And now the value is accessable within your Action!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.