1

When I am sending data (a string value) to my ASP.Net MVC controller the data is sent successfully. When I send special characters, like . and &, it shows an error like this:

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly)

Why does this happen? I can't find a solution for this. Please help me to resolve this problem.

$(function() {
    var link = '@Url.Action("Index", "Home", new {id= "_Description_"})';
    $('#btnSaveComments').click(function() {
        var description = $('#descr').val();           
        $('#btnSaveComments').attr('href', link.replace('_Description_', description));
    });
});
@Html.TextAreaFor(model => model.Description, new { id = "descr" })              
<button type="submit" text="submit" id="btnSaveComments" />                  
public ActionResult Index(string id)
{
    return view();
}                                                       

2 Answers 2

1

You need to encode the string, which you can do by using encodeURIComponent:

$('#btnSaveComments').attr('href', link.replace('_Description_', encodeURIComponent(description)));

Note however that the href attribute is not valid on button elements. I believe you may instead want to set the action attribute of your form. Either way, the logic is the same.

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

2 Comments

Thank you for reply Rory McCrossan,This code also not working getting same error.
Given the information you've shown it should work fine. Check your routing to ensure that the path being generated is correct.
0

I had the same error message when sending special characters in Url. You need to encode url string properly. Encapsulate in encodeURIComponent

var description = encodeURIComponent($('#descr').val());

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.