簡體   English   中英

將值從QueryString傳遞到ASP.NET MVC中的控制器的Index操作

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

我有這樣的查詢字符串:

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&....

我想將Months,SelectedMonth,Year和SelectedYear值傳遞到控制器的Index()中(Index是一個函數,接受0參數)。 另一個問題是,在Index函數完成之后,我希望運行綁定函數(在javascript中),以便按querystring中的SelectedMonth,SelectedYear將值綁定到dropdownlist中

請幫忙。 此功能有助於通過QueryString訪問視圖(而不是通過我的網站)非常感謝。

首先,動作名稱錯誤。 您應該使用以下內容,

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 / Skill?Months = 1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth = 8&Year = 2016,2017,2018&SelectedYear = 2016& ....

其次,您需要傳遞一些參數。 方法如下:

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

}

記住在您希望與它們一起使用時傳遞值。 如果不這樣做,您將面臨一些錯誤。 使用try catch塊來防止和處理異常。

您可能還會在訪問網頁時遇到異常。 嘗試放置可選參數,而不使用上面的參數。

 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)
{

}

或使用Reqest.QueryString

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

}

您需要從這里獲取參數HttpContext.Current.Request.QueryString

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM