1

I have a simple piece of HTML code which is supposed to call C# function on a press of a button, but it looks like the URL is bad, can anyone help me with Razor syntax? So far I have the following:

<div class="test1">
<div class="col-md-4">
    <h1>Some button</h1>
    <input type="button" value="Create" onclick="location.href='@Url.Action("addAdress", "AdressController")'" />
    <p></p>
</div>

And here is the Controller that is supposed to do something, name of the Controller is AdressController

private void addAdress()
    {
        Adress a = new Adress();

        a.Number = "1";
        a.Distance = 100;

        Dictionary<string, object> queryDict = new Dictionary<string, object>();
        queryDict.Add("Number", a.Number);
        queryDict.Add("Distance", a.Distance);

        var query = new Neo4jClient.Cypher.CypherQuery("CREATE (n:Adress {Number:'" + a.Number + "', Distance:'" + a.Distance + "'}) return n",
                                                        queryDict, CypherResultMode.Set);

        List<Adress> adrese = ((IRawGraphClient)client).ExecuteGetCypherResults<Adress>(query).ToList();

        /*
        foreach (Adress a1 in adrese)
        {
            MessageBox.Show(a1.Number);
        }
        */
    }
3
  • Try this : <input type="button" value="Create" onclick="location.href='@Url.Action("addAdress", "Adress")'" /> Commented Dec 27, 2017 at 16:27
  • 4
    You showed us your "controller method" but it's not an action method. Seems like you need to review the basics of MVC. You should return an ActionResult, your method should be public, and you should have routing configured for it. Commented Dec 27, 2017 at 16:29
  • well href will expect a url if you don't want that then use preventDefault() and cont. with your operation Commented Dec 28, 2017 at 10:11

1 Answer 1

2

Your controller Method is private, try changing to public

Sugestion

If you are going to change location.href on click, just wrap the input inside a link:

<a href='@Url.Action("addAdress", "Adress")'>
    <input type="button" value="Click Me">
</a>
Sign up to request clarification or add additional context in comments.

2 Comments

It also needs to be "Address" not "AddressController"
Yes, that was the solution I needed, much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.