开发者

calculate button pressed - retrieve rates from database

I need to link to the database (finance rates) when someone enters a number in the text box and clicks calculate that it will pull the rates from the database and display the calculation below in a 'form message'. What should I put in the homecontroller/index to link the code to the database?

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>

Home Controller:

    public ActionResult Index()
    {开发者_如何转开发
        financeInit();
        if (Request.Params["submitter"] == "calculate")
            calculatepressed();
        return View();

    public void calculatepressed()
    {
    .............

    }


I would wrap your fields in a form like this:

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

Then have your controller something like this:

    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();
    }

If this ever expands from a simple page you might want to consider changing the form action to a post and having two different methods handling the initial view of the home page and the a view for the results of the calculation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜