MVC id值始终为null [英] MVC id Value always null

查看:123
本文介绍了MVC id值始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET MVC的新手。我创建了Home控制器和动作调用showall with parameter但是当它点击动作参数时总是为null

路由图是默认的:

I am new to the ASP.NET MVC. I have created the Home controller and action call showall with parameter but when it hit action parameter allways null
route map is default:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }


            );





.................



.................

public ActionResult  showall (int? x)
{
    //this x is all ways null
 if(x==null)
 {
    ViewBag.id=0;
 }
else
 {
     ViewBag.id=x;
 }
    return View();



}


}

推荐答案

似乎是因为您在调用该操作时未在浏览器中使用正确的URL格式。默认情况下,在运行应用程序URL时,类似于http:// localhost:32215 /,在这种情况下,您将获得ID为Null。



如果我们写道:http:// localhost:32215 / Home / index / 2321321

然后我将2321321作为Id 。 (将localhost:32215替换为您的默认地址)



谢谢。
It seems because you are not using proper URL format in browser while calling that action. By default, while running the application URL would be something like "http://localhost:32215/" and in this case you will get Null for id.

If we write: http://localhost:32215/Home/index/2321321
Then I am getting 2321321 as Id. (Replace "localhost:32215" with your default address)

Thanks.


您好,



id总是为空,因为你可能没有正确使用来分配id。



首先你有参数写的是 int? x

您在路线中提到的MVC路线的默认行为是

id = UrlParameter.Optional 。所以你的参数应该是id而不是x。



如果你想使用x,那么你应该访问你的网址: -

Hello,

The id is always coming null because you may not be using properly to assign the id.

First of all the parameter you have written is int? x
The default behaviour of MVC route you have mentioned in the route is
id = UrlParameter.Optional . So you should have your parameter as id not x.

If you wish to use x, then you should access your url like:-
/Home/showAll?x=123





然后当动作方法调试器被击中时你会看到值123.

我希望你明白我的意思。

请回复你的查询,如果有的话。

谢谢



Then you will see the value 123 when the action method debugger gets hit.
I hope you understand what I meant.
Please post back your queries if any.
Thanks


这篇关于MVC id值始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆