-1

I am trying to pass an int value from a View to a class, which later calls another function based on the int value. The class I'm calling is called DataFetch.cs which has a function named GetLogs(int x);

What I have in the View is;

@Html.ActionLink("Connect",
            "GetLogs()", "DataFetch", new {id = item.MacNum}, new { @class = "DataFetch" })

^This gives me the url:

http://localhost:xxx/DataFetch/GetLogs()/1

Which obviously isn't what I want.

UPDATE:

function GetCloud()
    {
        console.log("insidefunc");
        $.ajax({
            type: "POST",
            url: '/DataFetch/GetLogs?id=' + 1,

            data: 1,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert("done")
            },
            failure: function (response) {
                alert("Failed: " + response.responseText);
            },
            error: function (response) {
                alert("Error: " + response.responseText);
            }
        });
    }

This did not work either.

  • 1
    The question in its current state is unclear as it is incomplete and would require too many questions to clarify what is being asked. Read [ask] and then provide a [mcve] that can be used to reproduce your problem, allowing us to better understand what is being asked. – Nkosi Dec 26 '17 at 11:04
  • @Nkosi an HtmlActionLink that calls a class which has a function in it with an int parameter, how is this not clear? –  Dec 26 '17 at 11:06
  • You have not provided the desired url. You basically showed. This is what I have, this is what is gave me but I do not want that. – Nkosi Dec 26 '17 at 11:07
  • @Nkosi something like this? `http://localhost:xxx/DataFetch/GetLogs(1)` –  Dec 26 '17 at 11:08
  • That is not valid. Based on the code snippet provided, the resulting url is correct for what was given. Hence the confusion when you say it is not what you wanted – Nkosi Dec 26 '17 at 11:10
  • 2
    That fact that people are answering your question incorrectly represents how unclear the question is. Refer to my original comment. Not much help can be provided till the question has been clarified and proper information provided. – Nkosi Dec 26 '17 at 11:12
  • 1
    He wishes to call a class and a method which are not controller and action through ActionLink. this is not possible as it is not the intend of Razor ActionLink. He will have to you an ajax call to the class. – Barr J Dec 26 '17 at 11:12
  • 1
    again this: http://localhost:xxx/DataFetch/GetLogs(1) is not valid!!! you cannot do this through action link or ajax! you need to learn more about mvc routing and webapi as well. – Barr J Dec 26 '17 at 11:14
  • @BarrJ that is exactly what I want. –  Dec 26 '17 at 11:23
  • Possible duplicate of [HTML.ActionLink method](https://stackoverflow.com/questions/200476/html-actionlink-method) – Nitesh Kumar Dec 26 '17 at 12:53

3 Answers3

1

Yes:

 @Html.ActionLink("ClickMe",  // link text
                     "Index", // action name
                     "Home",  // controller 
                     new { id = 2131 }, // (optional) route values
                     new { @class = "someClass" }) // html attributes

In order to learn more about ajax routing and calls use this:

Jquery ajax tutorial

Call a method with jquery ajax

This will give you enough info to begin with.

Community
  • 1
  • 1
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • This isn't what I asked. I saw this here already: https://stackoverflow.com/questions/5907530/can-i-add-a-class-to-an-html-actionlink-in-mvc3 –  Dec 26 '17 at 11:07
  • It is exactly what you asked: "the int value. The class I'm calling is called DataFetch.cs which has a function named GetLogs" the class, is the controller. The method is the Action. You cannot call a class which is not a controller and a method which is not an action in @Html.ActionLink. you will have to use ajax. Look at the method definition. – Barr J Dec 26 '17 at 11:10
  • this is the syntax to pass values from view - to model - to cotroller or view - to action method. You do not specify it in the method but in the url - you issue a get request. please read more about routing before asking questions like this. – Barr J Dec 26 '17 at 11:25
  • This will give me this: http://localhost:2078/Home/Index/1 but I do not intend to goto Home controller or its Index method. I want it to goto a class name DataFetch and then its method called GetLogs and pass the value `1` to that method. –  Dec 26 '17 at 11:26
  • excellent, then you will have to use jquery.Ajax. you cannot navigate to a non controller class and non action method thourgh razor. – Barr J Dec 26 '17 at 11:28
  • any example or link to learn from? –  Dec 26 '17 at 11:29
  • I will edit my answer for you :) – Barr J Dec 26 '17 at 11:30
  • can you write that function? I tried it but it throws the same controller error. –  Dec 26 '17 at 12:04
  • you will need to add the class an method code to your question – Barr J Dec 26 '17 at 12:08
1

You Can Directly Go to that Function By This ! this will also provide the parameter value which is required by that function

" @Url.Action("GetLogs","DataFetch",new { id = item.MacNum} ) "

for using this you have to create an anchor tag

<a href="@Url.Action("GetLogs","DataFetch",new { id = item.MacNum} )"> Connect</a>

if you want any thing else don't Vote down my answer ! just let me know in comments i will do that

  • @Html.ActionLink is an anchor tag! This is the difference between url.action and html.ActionLink! the behavior is the same! – Barr J Dec 26 '17 at 11:16
  • But it will call your function GetLogs in the class DataFetch and it will provide a int value ! what exactly you want – Syed Muhammad Munis Ali Dec 26 '17 at 11:19
  • Just as Html.ActionLink! but you will not have to create an anchor tag because ActionLink will do it, it is meant for it! his code is good, he does not understand how routing works. meaning, this is not a code issue at all! – Barr J Dec 26 '17 at 11:22
  • could u please write a url where u wanna go ? either what i understand is that ! you are just using "()" in your action link in place of action value which is not correct ! – Syed Muhammad Munis Ali Dec 26 '17 at 11:27
  • @SyedMunisAli I want it to goto a class name DataFetch and then its method called GetLogs and pass the value 1 to that method –  Dec 26 '17 at 11:28
  • just remove these "()" from GetLogs() it will do that ! – Syed Muhammad Munis Ali Dec 26 '17 at 11:31
0
@Html.ActionLink("Click Here",  // link text
                     "GetMyLogs", // you action name
                     "Home",  // you controller name
                     new { id = 1 }, // (optional) route values
                     new { @class = "someClass" }) 

well, what i suggest you to do is, on the click of the above ActionLink you should goto some action(your action) in a contoller(your controller) and in that action, you should create object of class(which you want to access) like this

public ActionResult GetMyLogs(int id){
    DataFetch Object_DataFetch  = new DataFetch();
    ObjectMyClass.GetLogs(1);
....
....
return "";
}

Hope this will help