我有一个详细信息页面,其中包含名为 UserId 的表单字段。在同一页面上,我有另一个搜索表单,其中有一个也名为 UserId 的字段。
我在两个不同的视图模型上使用 Html.LabelFor(vm > vm.UserId) 和 Html.TextBoxFor(sm > sm.UserId),vm 是视图模型,sm 是搜索模型。 (是的,两个模型上的 UserId 属性具有相同的名称 - 因为它们是相同的域属性。
当我导航到该页面时,虚拟机上填充的 UserId 被插入到由 MVC 命名为 UserId 的两个表单字段中。 UserId 为空。
这是我最初的问题。我的解决方案是使用 sm 的 Prefix 标志,
[HttpGet]
public ActionResult Search([Bind(Prefix = "Search")] SearchFormViewModel searchFormViewModel, PagingViewModel pagingViewModel)
{
这将促使 MVC 在搜索表单中的字段名称上呈现 Search.UserId 。代码中的属性仍将命名为 UserId。
这个解决方案似乎效果很好,
但是:
现在我必须解决来自 Global.asax 的路由上的 search.UserId 问题。
我像这样映射路线:
routes.MapRoute(
"MyRouteName",
"ControllerName/User/{Search.UserId}",
new { controller = "ControllerName", action = "Search" }
);
我的问题是 MVC 无法映射 Search.UserId(因为 .)以适合上面显示的操作中的 UserId(以 Search 为前缀)。
所以看起来 MVC 有一个前缀功能,实际上 nok 通过路由处理程序完全支持。
当然,我可以将 Search.UserId 重命名为 Search_UserId,但是该名称与 MVC 在上面的接收操作中期望的名称不匹配。 (需要 Search.UserId)重命名搜索模型的 UserId 属性可以解决该问题,但由于它在域中具有相同的值,因此这似乎是一种解决方法。
我是否在这里遗漏了有关前缀功能使用的某些内容,或者这根本不可能?
I have a details page containing a form field named UserId. On the same page i have another search form with a field also named UserId.
I am using Html.LabelFor(vm > vm.UserId) and Html.TextBoxFor(sm > sm.UserId) on the two different view models, vm being the view model and sm being the search model. (Yes, the UserId property on the two models has identical names - because they are the same domain property.
When i navigate to the page, the populated UserId on the vm is inserted into BOTH form fields named UserId by MVC. Even the sm.UserId are empty.
That is my initial problem. There are a few ways ti avoid that. My solution was to use the Prefix flag for the sm.
[HttpGet]
public ActionResult Search([Bind(Prefix = "Search")] SearchFormViewModel searchFormViewModel, PagingViewModel pagingViewModel)
{
This will provoke MVC to render a Search.UserId on the fieldname in the search form, but the property in code will still be named UserId.
This solution seems to work great!
BUT:
Now i have to address the search.UserId on a route from Global.asax.
I map the route like this:
routes.MapRoute(
"MyRouteName",
"ControllerName/User/{Search.UserId}",
new { controller = "ControllerName", action = "Search" }
);
My problem is that MVC can't map the Search.UserId (because of the .) to fit the UserId (prefixed with Search) in the action shown above.
So it seems like MVC has a prefix-feature, that are actually nok fully supported through the Route-handler.
Ofcourse i could rename the Search.UserId to Search_UserId, but then the name dosent match the name MVC expects in the recieving action above. (expects Search.UserId) Renaming The UserId property of the search model would fix the issue, but since it is the same value in the domain, this seems like a workaround.
Am I missing something here about the usage of the Prefix feature or is this just not possible?
发布评论
评论(1)
所以...我已经思考这个问题有一段时间了。 - 我的一位同事突然给我带来了光明。
问题在于 MVC 将对象映射到路由字典的位置。
不会工作。因为MVC无法处理对象名称中的.(点)。
但是由于对象名称只是路由值字典中的一个字符串键,因此我自己映射了它:
(){ "Search.UserId", Model.UserId })) % >” target="_blank">查看用户
,
So... I've been thinking about this for a while now. - And a colleague of mine suddently showed me the light.
The problem lies where MVC maps the object to a route dictionary.
wount work. Because MVC can not handle the .(dot) in the object name.
but since the object name is just a string key in the routevaluedictionary, mapping it my self did the trick:
<a href="<%: Url.RouteUrl("MyRouteName", new RouteValueDictionary(new Dictionary<string, object>(){ "Search.UserId", Model.UserId })) %>" target="_blank">See the user</a>