Using Razor to call C# function

c# asp.net-mvc razor

516 观看

1回复

13 作者的声誉

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);
        }
        */
    }
作者: Milos Stamenkovic 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

1650 作者的声誉

决定

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>
作者: Matheus Cuba 发布者: 2017 年 12 月 27 日
32x32