最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Window.location.href post parameters to actionresult asp.net mvc - Stack Overflow

programmeradmin4浏览0评论

I try to post textbox value to actionresult in asp mvc

Javascript:

function OnButtonClick() {
    var data= {
        TextBox: TextBox.GetValue()
    };
    var PostData= data.TextBox;

    window.location.href = "Home/MyActionResult?Page=data" + PostData;
}

ActionResult

public ActionResult MyActionResult(string PostData)
{
    return view();
}

Whenever I post data to Home/MyACtionResult , PostData is always null,

What am I missing ?

How can I post textbox value to actionresult?

I try to post textbox value to actionresult in asp.net mvc

Javascript:

function OnButtonClick() {
    var data= {
        TextBox: TextBox.GetValue()
    };
    var PostData= data.TextBox;

    window.location.href = "Home/MyActionResult?Page=data" + PostData;
}

ActionResult

public ActionResult MyActionResult(string PostData)
{
    return view();
}

Whenever I post data to Home/MyACtionResult , PostData is always null,

What am I missing ?

How can I post textbox value to actionresult?

Share Improve this question edited Mar 6, 2014 at 20:18 MikeSmithDev 15.8k4 gold badges60 silver badges91 bronze badges asked Mar 6, 2014 at 19:56 user3389856user3389856 911 gold badge1 silver badge2 bronze badges 4
  • location.href is a GET, use Ajax – mplungjan Commented Mar 6, 2014 at 20:00
  • if i use ajax , i can not change url , i want to use window.location – user3389856 Commented Mar 6, 2014 at 20:02
  • Of course you can. Do some research. Jquery history for example. You need to EITHER use Ajax or a form with method post OR change the server to accept a GET – mplungjan Commented Mar 7, 2014 at 6:06
  • @user3389856 are you trying to do a POST or a GET? – thepirat000 Commented Mar 7, 2014 at 18:44
Add a comment  | 

4 Answers 4

Reset to default 5

Try with this:

window.location.href = "Home/MyActionResult?Page=data&PostData=" + PostData;

Try this

var url = '@Url.Action("../Home/MyActionResult")' + '?Page='+data+'&'+PostData;
window.location.href = url;

This type of passing data is a bad approach. Please try to look into another code approach. This will not work for huge data, urls, secured data.

With window.location.href you can't use POST method. But here is simple trick if you don't want to you GET method.

You can use cookies and then 
function OnButtonClick() {
    setCookie("param1",value1,30);
    setCookie("param2",value2,30); // and so on, fetch input names and values using javascript 
    window.location.href = "Home/MyActionResult"
}
And then you can get cookies values from your MyActionResult page and use it.

Ofcource you have to include setCookie function as well in your page.

发布评论

评论列表(0)

  1. 暂无评论