window.location.href send multiple parameters asp.net mvc

喜夏-厌秋 提交于 2019-12-21 20:36:51

问题


I try to post multiple data to actionresult.

function OnButtonClick() {
        var data = {
            TextBox1: TextBox1.GetValue(),
            TextBox2: TextBox2.GetValue()
        };

        var PostData1 = data.TextBox1;
        var PostData2 = data.TextBox2;

window.location.href ="Home/MyActionResult?Page=data&Post=" + PostData1 + PostData2 ;
    }

ActionResult:

public ActionResult MyActionResult(string PostData1, string PostData2)
    {
     return view();
    }

I can send PostData1 value however i can not send PostData2 value to actionresult.So how can i send PostData1 and PostData2 values together by using window.location.href ?


回答1:


Are you trying to pass values of TextBox1 and TextBox2 to the controller action?

Use the following code to check if PostData1 and PostData2 have values.

alert(PostData1);
alert(PostData2);

If they do not have values then make use of Javascript or JQuery to fetch your textbox values and store them in Javascript variables, var PostData1 and PostData2.

If it has correct values proceed below.

You may try the following to check if it works

window.location.href ="Home/MyActionResult?PostData1=SomeThing1&PostData2=SomeThing2";

If it does works then instead of the following code

window.location.href ="Home/MyActionResult?Page=data&Post=" + PostData1 + PostData2 ;

try this

window.location.href ="Home/MyActionResult?PostData1=" + PostData1 + "&PostData2=" + PostData2;

Let me know if you get stuck somewhere.




回答2:


This will work but make sure value1 and value2 should be not null.

window.location.href = "add-categories.php?PostData1=" + value1 + "&PostData2=" + value2;



来源:https://stackoverflow.com/questions/22252184/window-location-href-send-multiple-parameters-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!