使用Razor调用C#函数

时间:2017-12-27 16:22:56

标签: c# asp.net-mvc razor

我有一段简单的HTML代码,只需按一下按钮即可调用C#函数,但看起来URL很糟糕,任何人都可以帮我使用Razor语法吗?到目前为止,我有以下内容:

<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>

这是控制器应该做的事情,控制器的名称是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);
        }
        */
    }

1 个答案:

答案 0 :(得分:1)

您的控制器方法为private,请尝试更改为public

Sugestion

如果您要在点击时更改location.href,只需将输入包装在链接中:

<a href='@Url.Action("addAdress", "Adress")'>
    <input type="button" value="Click Me">
</a>