0

I have a MVC3 action method with 3 parameters like this:

 var url = "/Question/Insert?" + "_strTitle='" + title + "'&_strContent='" + content + "'&_listTags='" + listTags.toString() + "'";

and I want to call this by normal javascript function not AJAX (because it's not necessary to use AJAX function) I tried to use this function but it didn't work:

window.location.assign(url);

It didn't jump to Insert action of QuestionController.

Is there someone would like to help me? Thanks a lot

This is more detail

I want to insert new Question to database, but I must get data from CKeditor, so I have to use this function below to get and validate data

// insert new question
$("#btnDangCauHoi").click(function () {
    //validate input data
    //chủ đề câu hỏi
    var title = $("#txtTitle").val();

    if (title == "") {
        alert("bạn chưa nhập chủ đề câu hỏi");
        return;
    }
    //nội dung câu hỏi
    var content = GetContents();
    content = "xyz";
    if (content == "") {
        alert("bạn chưa nhập nội dung câu hỏi");
        return;
    }
    //danh sách Tag
    var listTags = new Array();
    var Tags = $("#list_tag").children();

    if (Tags.length == 0) {
        alert("bạn chưa chọn tag cho câu hỏi");
        return;
    }

    for (var i = 0; i < Tags.length; i++) {
        var id = Tags[i].id;
        listTags[i] = id;
        //var e = listTags[i];
    }
    var data = {
        "_strTitle": title,
        "_strContent": content,
        "_listTags": listTags.toString()
    };
    //        $.post(url, data, function (result) {
    //            alert(result);
    //        });
    var url = "/Question/Insert?" + "_strTitle='" + title + "'&_strContent='" + content + "'&_listTags='" + listTags.toString() + "'";
    window.location.assign(url); // I try to use this, and window.location also but they're not working
});

This URL call MVC action "Insert" below by POST method

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Insert(string _strTitle, string _strContent, string _listTags)
    {

        try
        {
            //some code here 
        }
        catch(Exception ex)
        {
            //if some error come up
            ViewBag.Message = ex.Message;
            return View("Error");
        }
        // if insert new question success
        return RedirectToAction("Index","Question");
    }

If insert action success, it will redirect to index page where listing all question include new question is already inserted. If not, it will show error page. So, that's reason I don't use AJAX

Is there some one help me? Thanks :)

4
  • Why don't you want to use AJAX?
    – jrummell
    May 4, 2012 at 14:33
  • Did you get an error or did the page just not navigate? Is your controller action a POST or GET?
    – Terry
    May 4, 2012 at 14:36
  • Maybe you can give us the syntax of your Route and Action method? The window.location should work, unless your routing is not correct. Do you get any errors.
    – Ronald
    May 4, 2012 at 14:43
  • What kind of result does your controller method return? If it's not HTML content type, the browser might react funky when you try anything that's not an out-of-band call.
    – Rich
    May 4, 2012 at 14:49

3 Answers 3

2

Try:

window.location = yourUrl;

Also, try and use Fiddler or some other similar tool to see whether the redirection takes place.

EDIT:

You action is expecting an HTTP POST method, but using window.location will cause GET method. That is the reason why your action is never called.

[HttpPost] 
[ValidateInput(false)] 
public ActionResult Insert(string _strTitle, string _strContent, string _listTags)
{
    // Your code
}

Either change to HttpGet (which you should not) or use jQuery or other library that support Ajax in order to perform POST. You should not use GET method to update data. It will cause so many security problems for your that you would not know where to start with when tackling the problem.

Considering that you are already using jQuery, you might as well go all the way and use Ajax. Use $.post() method to perform HTTP POST operation.

Inside a callback function of the $.post() you can return false at the end in order to prevent redirection to Error or Index views.

$.post("your_url", function() {
    // Do something

    return false; // prevents redirection
});

That's about it.

7
  • That does the exact same thing as window.location.assign() May 4, 2012 at 14:37
  • This is true, but I had an issue once with one version of Chrome where .assign did not work properly but window.location was quite fine. I never figured out why as I didn't want to bother with it too much. I got the end result with windows.location = "url"
    – Huske
    May 4, 2012 at 14:48
  • Oh, really ? Fair enough, window.location is way more widely used anyways. May 4, 2012 at 14:55
  • I already this window.location, but it's not working the way U want, it just open the same page on so many tab in browser. But thanks for your advice :)
    – Abigail Vo
    May 4, 2012 at 15:04
  • @Huske, what should I do? Is there some advice? Thanks :)
    – Abigail Vo
    May 4, 2012 at 15:10
1

You could try changing

var url = "/Question/Insert?" + "_strTitle='" + title + "'&_strContent='" + content + "'&_listTags='" + listTags.toString() + "'";

to

var url = "/Question/Insert?_strTitle=" + title + "&_strContent=" + content + "&_listTags=" + listTags.toString();

I've removed the single quotes as they're not required.

Without seeing your php code though it's not easy to work out where the problem is.

When you say "It didn't jump to Insert action of QuestionController." do you mean that the browser didn't load that page or that when the url was loaded it didn't route to the expected controller/action?

2
  • I mean it didn't route to the expected Controller/Action. Is there some advice, @Jimbo?
    – Abigail Vo
    May 4, 2012 at 15:09
  • I would say it's not a problem with your clientside scripts, if the url is being loaded by the browser but your serverside application is not routing it correctly it must be something to do with the way the URL is formatted. You could try to add a slash after the Insert part of the URL: /Question/Insert/?_strTitle= which in my experience has caused issues in the past with some MVC frameworks. I hope that's of some help.
    – Jimbo
    May 8, 2012 at 9:27
0

You could use an iframe if you want to avoid using AJAX, but I would recommend using AJAX

<iframe src="" id="loader"></iframe>
<script>
document.getElementById("loader").src = url;
</script>
1
  • Why would anyone down vote this answer when it actually achieves the desired result?
    – Bloafer
    May 4, 2012 at 14:53

Your Answer

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

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