简体   繁体   English

将值从QueryString传递到ASP.NET MVC中的控制器的Index操作

[英]Passing value from QueryString into Index action of controller in asp.net mvc

I have a querystring like this: 我有这样的查询字符串:

http://localhost:2563/Skill?Months=1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth=8&Year=2016,2017,2018&SelectedYear=2016&....

And I want to pass Months, SelectedMonth, Year, SelectedYear value into Index() of controller (Index is a function takes 0 argument). 我想将Months,SelectedMonth,Year和SelectedYear值传递到控制器的Index()中(Index是一个函数,接受0参数)。 And another issue, after Index function completed, I want binding function (in javascript) to run to bind value into dropdownlist by the SelectedMonth, SelectedYear in querystring 另一个问题是,在Index函数完成之后,我希望运行绑定函数(在javascript中),以便按querystring中的SelectedMonth,SelectedYear将值绑定到dropdownlist中

Please help. 请帮忙。 This function helps access the Views by QueryString (not through my website) Many thanks. 此功能有助于通过QueryString访问视图(而不是通过我的网站)非常感谢。

First, the action name is wrong. 首先,动作名称错误。 you should use the following, 您应该使用以下内容,

http://localhost:2563/index?Months=1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth=8&Year=2016,2017,2018&SelectedYear=2016& ... http:// localhost:2563 / index?Months = 1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth = 8&Year = 2016,2017,2018&SelectedYear = 2016& ...

instead of 代替

http://localhost:2563/Skill?Months=1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth=8&Year=2016,2017,2018&SelectedYear=2016& .... http:// localhost:2563 / Skill?Months = 1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth = 8&Year = 2016,2017,2018&SelectedYear = 2016& ....

second, You need to pass some parameters. 其次,您需要传递一些参数。 Here is how: 方法如下:

public ActionResult Index(List<int> Months,int SelectedMonth,List<int> Year, int Year)
{

}

remember to pass values as you wish to work with them. 记住在您希望与它们一起使用时传递值。 if you don't, you will face some error. 如果不这样做,您将面临一些错误。 use try catch block to prevent and handle exceptions. 使用try catch块来防止和处理异常。

You may also face exception accessing the web page. 您可能还会在访问网页时遇到异常。 try to put optional parameter instead of using the above one. 尝试放置可选参数,而不使用上面的参数。

 public ActionResult Index(List<int>? Months,int? SelectedMonth,List<int>? Year, int? Year)
    {

    }
public ActionResult Index(List<int> Months,int SelectedMonth,List<int> Year, int Year)
{

}

or use Reqest.QueryString 或使用Reqest.QueryString

public ActionResult Index()
{
    var months=Reqest.QueryString["Months"];
    .
    .
    .

}

您需要从这里获取参数HttpContext.Current.Request.QueryString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从javascript传递到asp.net MVC中的操作方法 - Passing data from javascript to action method in asp.net MVC asp.net mvc 3从控制器动作调用javascript - asp.net mvc 3 call javascript from controller action 从asp.net mvc控制器操作获取查询字符串的值,以<script type=“text/javascript”> - Get value of a query string from asp.net mvc controller action to <script type=“text/javascript”> 将数据从Ajax传递到ASP.NET MVC中的控制器 - Passing data from Ajax to controller in asp.net mvc 将 javascript 数组从视图传递到 ASP.Net MVC 5 中的控制器 - Passing javascript array from view to controller in ASP.Net MVC 5 重定向到同一控制器中的另一个动作并传递一些数据asp.net mvc3 - Redirecting to another action in same controller and passing some data asp.net mvc3 MVC ASP.NET查询字符串不正确 - MVC ASP.NET querystring incorrect ASP.NET MVC:从JavaScript传递到MVC控制器操作后,二维数组为null - ASP.NET MVC : 2d array is null after being passed from javascript to MVC controller action 单击asp.net MVC中的按钮,如何将textarea值传递给控制器​​操作? - How to pass textarea value to controller action on the click of a button in asp.net mvc? 通过带参数的JavaScript重定向到动作控制器(ASP.NET MVC) - Redirect to action controller by JavaScript with parameters (ASP.NET MVC)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM