![](/img/trans.png)
[英]Taking default value of Int when parameter is passed from Angular service from WebApi controller method
[英]unable to get parameter value in controller which is passed from the url
我想將動態值傳遞給控制器,例如querystring。 我傳遞了一個字符串,但是我在控制器中得到了null。 我點擊了網址“ http:// localhost:62666 / tracker / MapDetails / sss ”,但未在控制器中獲得任何值。 您能指出我做錯了什么嗎?
控制器:
public ActionResult MapDetails(string val)
{
AssetTracker model = new AssetTracker();
model.deviceid = val;
return View(model);
}
視圖:
@model smartpond.Controllers.AssetTracker
@{
ViewBag.Title = "MapDetails";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>
My device id is : @Model.deviceid
</h1>
您會在控制器中獲得Null值,因為MVC中的模型綁定程序無法綁定您傳遞給MVC路由的變量,因此,您需要添加滿足傳遞參數的自定義路由,以將其標識為模型綁定器以添加您的自定義路由,必須將其寫入解決方案中app_start文件夾的RouteConfig.cs文件中,並確保將其附加到默認路由的頂部,並使用其他名稱。
routes.MapRoute(
name: "Overrided",
url: "{controller}/{action}/{val}",
defaults: new { controller = "Home", action = "Index", val= UrlParameter.Optional }
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.