2011-12-13 57 views
2

我想爲ActionResult方法創建一個可選參數。方法的MVC可選參數

我有以下幾點:

public ActionResult ViewReq (int id, string Req = null) 

當我試圖做到以下幾點:

http://localhost/RepMedia/Controller1/ViewReq?id=34343?Req="34233" 

我嘗試以下,但得到了一個錯誤:

An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

我不是確定我做錯了什麼。

回答

3

問題出在'id'。該ID必須是基本URI的一部分:

http://localhost/RepMedia/Controller1/ViewReq/34343?Req=34233 
2
http://localhost/RepMedia/Controller1/ViewReq?id=34343&Req=34233 

在第一個參數之前使用問號 - 所有其他人應該按照&符號拆分。

0

您不需要創建一個字符串參數可選,因爲它們是引用類型,如果它們沒有被MVC傳入,它們的值將爲空。該URL將以非空「Id」結尾,但爲空「Req」。

1
public ActionResult ViewReq (int? id, string Req) 

http://localhost/RepMedia/Controller1/ViewReq?id=34343&Req=34233