I have created a table in the database that has a System.Guid as it's primary key. The required ADO.Net Entity Framework model has been generated and the required stored procedures has been mapped.
我在数据库中创建了一个表,它有一个System.Guid作为它的主键。已生成所需的ADO.Net实体框架模型,并已映射所需的存储过程。
I created a new controller and added the basic required code for Create and Edit for the data. However when the clicking on the link to edit the a particular record the following error is generated:
我创建了一个新的控制器,并为数据的创建和编辑添加了基本的必需代码。但是,当单击链接以编辑特定记录时,会生成以下错误:
The parameters dictionary contains a null entry for parameter '[MyId]' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Edit(System.Guid)' in '[MySite].[MyController].[SpecificController]'. To make a parameter optional its type should be either a reference type or a Nullable type. Parameter name: parameters
参数字典包含'[MySite]中方法'System.Web.Mvc.ActionResult Edit(System.Guid)'的非可空类型'System.Guid'的参数'[MyId]'的空条目。[MyController] [SpecificController]”。要使参数可选,其类型应为引用类型或Nullable类型。参数名称:参数
The edit action is declared in the controller as follows:
编辑操作在控制器中声明如下:
public ActionResult Edit(Guid itemId)
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(MyItem itemToModify)
{
}
When adding a new record the new Guid is generated via a Stored Procedure and the list is displaying the correct Guid. The Url is also passing the correct Guid for retrieval.
添加新记录时,通过存储过程生成新Guid,列表显示正确的Guid。 Url也传递正确的Guid进行检索。
I can't seem to capture the point at which this fails, but how would I go about passing a System.Guid as a parameter to the Controller?
我似乎无法捕捉失败的重点,但我如何将System.Guid作为参数传递给Controller?
2 个解决方案
#1
17
Unless you've updated your routes, it is expecting (by default) the final parameter in a route to be named "id". That is, if you have a route like /specific/edit/5646-0767-..., it will map the guid into the route value dictionary with key "id" regardless of what the parameter on your method is named. I'd follow this convention and change the method definition to:
除非您已更新路由,否则(默认情况下)期望路由中的最终参数命名为“id”。也就是说,如果你有像/ specific / edit / 5646-0767 -...这样的路由,它会将guid映射到带有键“id”的路由值字典,而不管你方法中的参数是什么命名。我遵循这个约定并将方法定义更改为:
public ActionResult Edit(Guid id)
You can get around this, by explicitly specifying the name of the route parameter, but then you end up with a url that looks like: /specific/edit?itemid=5646-0767-...
您可以通过显式指定路由参数的名称来解决这个问题,但最后会得到一个类似于以下内容的网址:/ specific / edit?itemid = 5646-0767 -...
#2
4
Putting sample code below for anyone who may need it (I sure did)
为任何可能需要它的人提供下面的示例代码(我确定)
public ActionResult Profile(Guid? id)
{
if (!id.HasValue)
{
return View(new ProfileRepository().GetUserProfile(User.Identity.Name));
}
return View(new ProfileRepository().GetUserProfile(id.Value));
}
Thank you for the anwer above which led me in the right direction
谢谢你上面的通道,这使我朝着正确的方向前进
#1
17
Unless you've updated your routes, it is expecting (by default) the final parameter in a route to be named "id". That is, if you have a route like /specific/edit/5646-0767-..., it will map the guid into the route value dictionary with key "id" regardless of what the parameter on your method is named. I'd follow this convention and change the method definition to:
除非您已更新路由,否则(默认情况下)期望路由中的最终参数命名为“id”。也就是说,如果你有像/ specific / edit / 5646-0767 -...这样的路由,它会将guid映射到带有键“id”的路由值字典,而不管你方法中的参数是什么命名。我遵循这个约定并将方法定义更改为:
public ActionResult Edit(Guid id)
You can get around this, by explicitly specifying the name of the route parameter, but then you end up with a url that looks like: /specific/edit?itemid=5646-0767-...
您可以通过显式指定路由参数的名称来解决这个问题,但最后会得到一个类似于以下内容的网址:/ specific / edit?itemid = 5646-0767 -...
#2
4
Putting sample code below for anyone who may need it (I sure did)
为任何可能需要它的人提供下面的示例代码(我确定)
public ActionResult Profile(Guid? id)
{
if (!id.HasValue)
{
return View(new ProfileRepository().GetUserProfile(User.Identity.Name));
}
return View(new ProfileRepository().GetUserProfile(id.Value));
}
Thank you for the anwer above which led me in the right direction
谢谢你上面的通道,这使我朝着正确的方向前进