2013-06-26 60 views
2

我想要URL通過@ URL.Action與參數。 這是我的代碼,但我從它得到空白值。使用Javascript中的參數訪問@ URL.action

腳本

var OpenURL = ' @Url.Action("Document", "Controller", new { ID= "-22" })'; 
alert(OpenURL); // Value of this is coming blank 

Controller.cs

public ActionResult Document(int? ID) 
{ 
    return View(); 
} 

Route.cs

routes.MapRoute("Document", "OpenDocuments/{ID}", new { controller = "Controller", action = "Document", ID= UrlParameter.Optional }); 

請幫助我!

謝謝!

回答

0

ControllerController.cs

public class ControllerController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     public ActionResult Document(int? ID) 
     { 
      return View(); 
     } 
    } 

Index.cshtml:

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 
<script> 
    var OpenURL = ' @Url.Action("Document", "Controller", new { ID= "-22" })'; 
    alert(OpenURL); // Value of this is coming blank 
</script> 

作品在我的情況。