C# 按下计算按钮-从数据库检索速率

C# 按下计算按钮-从数据库检索速率,c#,asp.net-mvc,visual-studio,C#,Asp.net Mvc,Visual Studio,当有人在文本框中输入一个数字并单击“计算”时,我需要链接到数据库财务利率,它将从数据库中提取利率,并在下面的“表单消息”中显示计算结果。我应该在homecontroller/索引中放入什么来将代码链接到数据库 Index.aspx: <td>You wish to convert: <input type="text" name="amount" size="30" onblur="test_ifinteger(Index.amount,'amounts')"/

当有人在文本框中输入一个数字并单击“计算”时,我需要链接到数据库财务利率,它将从数据库中提取利率,并在下面的“表单消息”中显示计算结果。我应该在homecontroller/索引中放入什么来将代码链接到数据库

Index.aspx:

    <td>You wish to convert:
    <input type="text" name="amount" size="30" onblur="test_ifinteger(Index.amount,'amounts')"/>
    <input type="submit" name="submitter" value="calculate" />
    <tr><td colspan="2">That will produce:<%=ViewData["formmessage"] %></td></tr>

我会将您的字段包装成如下形式:

<form action="Home" method="get">
    <div>
        You wish to convert:
        <input type="text" name="amount" size="30" id="userValue" onblur=""test_ifinteger(Index.amount,'amounts')"/>
        <input type="submit" name="userSubmit" />
        <br />
        That will produce:<%=ViewData["formmessage"] %>     
    </div>
</form>
    public ActionResult Index()
    {
        int value;
        if (int.TryParse(Request.Params["amount"], out value))
        {
            ViewData["formmessage"] = calculatepressed(value);
        }
        return View();
    }

    private string calculatepressed(int value)
    {
        // Do your magic here and return the value you calculate
        return value.ToString();
    }

如果这从一个简单的页面扩展,您可能需要考虑将表单动作更改为一个POST,并使用两种不同的方法处理主页的初始视图和计算结果的A视图。

    public ActionResult Index()
    {
        int value;
        if (int.TryParse(Request.Params["amount"], out value))
        {
            ViewData["formmessage"] = calculatepressed(value);
        }
        return View();
    }

    private string calculatepressed(int value)
    {
        // Do your magic here and return the value you calculate
        return value.ToString();
    }