1

我刚刚完成了一个控制台应用程序的工作,我创建了一个贷款计算器。我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoanCalculator
{
    public class LoanCalculator
    {

        public static void Main(string[] args)
        {
            // declare variables
            double principle = 0; 
            double years = 0;
            double interest = 0;
            string principleInput, yearsInput, interestInput;


            // User input for Principle amount in dollars
            Console.Write("Enter the loan amount, in dollars(0000.00): ");
            principleInput = Console.ReadLine();
            principle = double.Parse(principleInput);
            //Prompt the user to reenter any illegal input
            if (principle < 0)
            {
                Console.WriteLine("The value for the mortgage cannot be a negative value");
                principle = 0;
            }


            // User input for number of years
            Console.Write("Enter the number of years: ");
            yearsInput = Console.ReadLine();
            years = double.Parse(yearsInput);
            //Prompt the user to reenter any illegal input
            if (years < 0)
            {
                Console.WriteLine("Years cannot be a negative value");
                years = 0;
            }

            // User input for interest rate
            Console.Write("Enter the interest rate(%): ");
            interestInput = Console.ReadLine();
            interest = double.Parse(interestInput);
            //Prompt the user to reenter any illegal input
            if (interest < 0)
            {
                Console.WriteLine("The value for the interest rate cannot be a negative value");
                interest = 0;
            }

            //Calculate the monthly payment
            //ADD IN THE .Net function call Math.pow(x, y) to compute xy (x raised to the y power). 
            double loanM = (interest / 1200.0);
            double numberMonths = years * 12;
            double negNumberMonths = 0 - numberMonths;
            double monthlyPayment = principle * loanM / (1 - System.Math.Pow((1 + loanM), negNumberMonths));

            //double totalPayment = monthlyPayment;


            //Output the result of the monthly payment
            Console.WriteLine(String.Format("The amount of the monthly payment is: {0}{1:0.00}", "$", monthlyPayment));
            Console.WriteLine();
            Console.WriteLine("Press the Enter key to end. . .");
            Console.Read();

        }
    }
}

上面的一切都按计划进行。我现在正在尝试使用 Visual Studio 将其转换为 ASP.NET Web 应用程序,但我被卡住了。我当前的 UI 设置了 3 个标签,用于原则输入(带有文本框)、贷款期限(带有单选按钮列表)和利率(带有下拉列表)。

我遇到的问题是我正在尝试为我的贷款期限(15、30 或其他)获取一个单选按钮列表作为我的选择。如果用户选择其他,我正在尝试实现一个文本框,让他们输入一个以年为单位的值。在用户选择他的预期持续时间后,我希望我的 InterestRate 成为一个下拉列表项,其中 1%-10% 作为选项。

我还有一个名为“计算”的按钮,它根据用户输入计算解决方案。如果有人能引导我走向正确的方向来解决这个问题。我是使用 ASP.NET 的新手,我很困惑如何成功地将我的控制台应用程序转换为 ASP.NET 项目。谢谢您的帮助!

我的页面如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title>Monthly Mortgage Calculator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Monthly Payment Loan Calculator</h1>
    </div>

     <asp:Label ID="Label1" runat="server" Text="Please enter the principle amount">     </asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtPrinciple" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" 
        Text="Please enter the loan duration in years"></asp:Label>
    <br />
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
        <asp:ListItem>15 Years</asp:ListItem>
        <asp:ListItem>30 Years</asp:ListItem>
        <asp:ListItem>Other</asp:ListItem>
    </asp:RadioButtonList>
    <br />
  nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n    bsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtYears" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Please select the interest rate">    </asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
        <asp:ListItem>5</asp:ListItem>
        <asp:ListItem>6</asp:ListItem>
        <asp:ListItem>7</asp:ListItem>
        <asp:ListItem>8</asp:ListItem>
        <asp:ListItem>9</asp:ListItem>
        <asp:ListItem>10</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Monthly Payment" />

    </form>
</body>
</html>
4

2 回答 2

2

以下是转换项目的一些一般步骤。如果您需要代码,请告诉我哪个位。

1)创建一个空的 Asp.net 应用程序(不是网页)

2)在标记中添加一些 asp.net 标签(不是后面的代码)它看起来像

          <asp:TextBox ID="tbYears" runat="server" />
          <asp:RadioButtonList ID="rblDuration" runat="server" >
                 <asp:ListItem Text="15" Value="15" />
                  ...
          </asp:RadioButtonList>
          <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

3)将任何启动代码放入您的 Page_Load 方法中

     protected void Page_Load(object sender, EventArgs e)
     {
     }

4)写你的按钮点击事件

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
          double years = double.parse(tbYears.text); // This will throw an exception if a user doesn't input a number
          var duration = rblDuration.SelectedValue // Needs to be cast too as it comes back as a string
    } 

这应该让你开始

编辑:

另一个提示:改变

           <asp:ListItem>15 Years</asp:ListItem>

进入

          <asp:ListItem Value="15" Text="15 Years" />

使解析值更容易

于 2013-05-30T15:13:36.567 回答
0

一般来说,您将能够使用相同的逻辑,除了我会将它转移到一个班级。

1)如果您有控制台输入,您将能够使用文本框。2)如果您在 WriteLine() 上有结果,您可能希望使用标签来显示结果。

ASP.NET 页面将有一个保存标记 (HTML) 的 .aspx 文件和一个保存代码隐藏的 .cs 文件。可以为页面元素提供将调用代码隐藏中的方法的事件。

例如,在设计视图中选择您的提交按钮,然后在“属性”窗格中查找事件按钮。您应该能够为按钮单击添加新事件。它将按钮的单击映射到代码隐藏中的方法:

protected void importBtn_Click(object sender, EventArgs e)
{
    Textbox1.Text = "Editing the Textbox contents";
}

您可以访问控件的属性,如图所示。

祝你好运,希望这会有帮助。

于 2013-05-30T15:16:15.947 回答