0

I wanted to pass textarea value and some other parameters to action method. so i did using the jquery in the following way.

View:-

@Html.TextArea("aboutme")
<a id="@Profile.Id" title="@Profile.name" onclick="SubmitProfile(this)" >
 Submit</a>

Jquery Method:-

function SubmitReview(e,count) {    
    var Text = "'"+jQuery("#aboutme").val()+"'";
    var url = 'http://' + window.location.host + '/Controller/ActionMethod?' + 'id=' + e.id       + '&name=' + e.title + '&aboutme=' + Text;
    jQuery("#Profile").load(url);

}

Action Method:-

public ActionResult ActionMethod(string id,string name,string aboutme)
        {
            //
        }

Above code is working fine, but when ever any one of the parameter value contains spaces in it. In Jquery method URL looks fine.but in action method it trims the value upto 1st space and remaining parameters are null.

Let me explain with some example

let say id='123',name='amith cho' ,aboutme='i am software engg'

Url in Jquery method

url='http://localhost/Controller/ActionMethod?id=123&name=amith cho ,aboutme=i am software engg'

but it is getting in action method as id=123 name=amith aboutme=null

How to solve this?

1
  • sending data to action using this url http://localhost/Controller/ActionMethod?id=123&name=amith cho ,aboutme=i am software engg is a bad, bad, bad practice... POST these values instead. Commented Nov 9, 2012 at 8:30

1 Answer 1

3

You should url encode the value of the input if you are passing it as a query parameter.

function SubmitReview(e,count) {
    var Text = jQuery("#aboutme").val(); // quotes aren't necessary
    var url = 'http://' + window.location.host + '/Controller/ActionMethod?' 
                   + 'id=' + e.id 
                   + '&name=' + encodeURIComponent(e.title) 
                   + '&aboutme=' + encodeURIComponent(Text); 
    jQuery("#Profile").load(url);

}

If your string contains extra characters not handled by encodeURIComponent, you can try this function, referenced from the MDN docs. Replace the calls to encodeURIComponent above with calls to this.

function fixedEncodeURIComponent (str) {
  return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}
Sign up to request clarification or add additional context in comments.

2 Comments

encodeURIComponent() not allowing these special characters ~!*()' so it is breaking...
@User_MVC - updated with alternative suggestion from the docs to include more escaped characters.

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.