[removed].href send multiple parameters asp.net mv

2019-05-11 03:10发布

问题:

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;