如何使用@ ajax.actionlink来处理2个参数? [英] How do I get @ajax.actionlink to work with 2 parameters?

查看:113
本文介绍了如何使用@ ajax.actionlink来处理2个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序,我使用下面的ActionLinks通过Web API在SQL Server中运行存储过程,并在div元素中显示结果。这两个参数是存储过程的输入参数。不要混淆,ObjType只是一个常规变量名,不是指OOP对象类型。





下面的ActionLink命令创建了这个URL,给出404(未找到)错误。请注意,它以不同方式处理这两个参数。

--------------------------------- --------------------------------------------------

http:// localhost:58641 / api / ControllerName / Get / 1?ObjType = Gizmo





但是,如果我手动将URL更改为此,它将起作用并返回所需的数据。

----------------------- -------------------------------------------------- ----------

http:// localhost:58641 / api / ControllerName / Get?id = 1& ObjType = Gizmo





问题:到目前为止搜索互联网对这个没有帮助。

我如何获得Ajax.ActionLink命令给我正确的网址?



我的尝试:



I have an MVC application in which I am using ActionLinks like the one below to run a stored procedure in SQL Server via a Web API and display the results in a div element. The two parameters are input parameters to the stored procedure. Not to confuse, ObjType is just a regular variable name, not referring to an OOP object type.


The below ActionLink command creates this URL, which gives a 404 (not found) error. Notice that it handles the two parameters differently.
-----------------------------------------------------------------------------------
http://localhost:58641/api/ControllerName/Get/1?ObjType=Gizmo


But if I change the URL manually to this, it works and returns the desired data.
-----------------------------------------------------------------------------------
http://localhost:58641/api/ControllerName/Get?id=1&ObjType=Gizmo


Question: Searching the internet so far hasn't helped on this one.
How can I get the Ajax.ActionLink command to give me the correct URL?

What I have tried:

@Ajax.ActionLink("LOCUS", "Get", "api/ControllerName", 
                  new { id = 1, ObjType = "Gizmo" },
                  AjaxOptions
	             HttpMethod = "GET",
	             UpdateTargetId = "divResult",
	             InsertionMode = InsertionMode.Replace
                })

推荐答案

这里:

Here:
new { id = 1, ObjType = "Gizmo" }



执行以下操作:


Do something like this:

new { id = 1, ObjType = "Gizmo", param1 = "value1", param2 = "value2" }



在服务器端,你应该有一个带有正确参数列表的Get方法...


On the server side you should have a Get method with the proper parameter list...

Get(string id, string ObjType, string param1, string param2)


已解决



如果我将第一个参数'id'命名为,我发现会导致问题。如果我将它命名为'id'并将



new(id = 1,objType ='Gizmo'),



然后网址出现



< controllername> /< actionname> / 1?ObjType = Gizmo



,控制器操作方法不接受。但如果我将其他名称命名为id或明显的关键字,例如ClientID,然后URL正确显示为



< controllername> /< actionname>?ClientID = 1& ObjType = Gizmo
SOLVED

I discovered that it causes a problem if I name the first parameter 'id'. If I name it 'id' and put

new (id = 1, objType = 'Gizmo'),

then the URL comes out as

<controllername>/<actionname>/1?ObjType=Gizmo

which is not accepted by the controller action method. But if I name it anything else besides 'id' or an obvious keyword, e.g. ClientID, then the URL comes out correctly as

<controllername>/<actionname>?ClientID=1&ObjType=Gizmo


这篇关于如何使用@ ajax.actionlink来处理2个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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