Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
@Ajax.ActionLink("LOCUS", "Get", "api/ControllerName", 
                  new { id = 1, ObjType = "Gizmo" },
                  AjaxOptions
	             HttpMethod = "GET",
	             UpdateTargetId = "divResult",
	             InsertionMode = InsertionMode.Replace
                })
Posted
Updated 19-Apr-18 19:54pm
v6

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

Do something like this:
C#
new { id = 1, ObjType = "Gizmo", param1 = "value1", param2 = "value2" }

On the server side you should have a Get method with the proper parameter list...
C#
Get(string id, string ObjType, string param1, string param2)
 
Share this answer
 
Comments
Member 12824529 18-Apr-18 23:17pm    
Thanks. Actually, id and objType are the two parameters. ObjType is probably not a good choice on my part, as it looks like I'm referring to an object type, but it's just a normal parameter. The issue I'm running into is the Ajax.ActionLink handles the first parameter differently than the rest. So instead of it appending after the action with ?id=1&objType=Gizmo, it appends with instead with /1?objType-Gizmo. It treats the first parameter like it's the action name.
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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900