在MVC Razor中传递url中的参数

时间:2018-03-12 08:48:49

标签: c# asp.net-mvc razor

我有一个按钮,我想在点击该按钮时向控制器发送一些值。

我这样做。

但这并没有导航到控制器。我通过在控制器中应用断点来检查它。

<input type="button" name="sendPass" value="Send" class="btn btn-primary" onclick="location.href='@Url.Action("Index", "Display")?mobile=' + @Model[i].Mobile" />

HTML -

<input type="button" name="sendPass" value="Send" class="btn btn-primary" onclick="location.href='/Display/Index?mobile=' + 9871790435">

控制器代码 -

public ActionResult Index(List<Registration> _ModelList, string sendPass, string submitChanges)
    {
        ViewBag.Msg = null;
            Registration _Model = new Registration();
        List<Registration> _mainModel = null;

        if (!string.IsNullOrEmpty(sendPass))
        {
            string username = "";
            string password = "";
            string sender = "";
            string dest = Request.QueryString["mobile"];//"9871790435";
            string msg = "";
            int priority = 1;

            using (var web = new System.Net.WebClient())
            {
                try
                {

                    string url = "https://www.prpsms.co.in/API/SendMsg.aspx?" +
                        "uname=" + System.Web.HttpUtility.UrlEncode(username) +
                         "&pass=" + password +
                         "&send=" + sender +
                        "&dest=" + dest +
                        "&msg=" + System.Web.HttpUtility.UrlEncode
                        (msg, System.Text.Encoding.GetEncoding("ISO-8859-1")) +
                        "&priority=" + priority;

                    WebRequest request = HttpWebRequest.Create(url);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream s = (Stream)response.GetResponseStream();
                    StreamReader readStream = new StreamReader(s);
                    string dataString = readStream.ReadToEnd();
                    response.Close();
                    s.Close();
                    readStream.Close();
                    ViewBag.Msg = "Password Sent successfully.";
                }
                catch (Exception ex)
                {

                }
            }
        }
    }

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试网址这样的行动:

@Url.Action("Index", "Display", new { mobile = @Model[i].Mobile})

完整按钮:

<input type="button" name="sendPass" value="Send" class="btn btn-primary" onclick="location.href='@Url.Action("Index", "Display", new { mobile = @Model[i].Mobile})'" />