控制台应用程序在c#到ASP.净的困难

本文关键字:ASP 应用程序 控制台 | 更新日期: 2023-09-27 18:16:13

我正在创建一个ASP。从我之前创建的控制台应用程序中删除。NET应用程序。我有问题,而试图做到这一点。控制台应用程序是一个计算器,用于查找贷款的每月付款。我被困在如何得到每月支付计算在ASP工作。网络编码。

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

我现在的页面标记代码是

<%@ 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" 
        ontextchanged="txtPrinciple_TextChanged"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" 
        Text="Please enter the loan duration in years"></asp:Label>
    <br />
    <asp:RadioButtonList ID="rblYears" runat="server" AutoPostBack="True" 
        onselectedindexchanged="rblYears_SelectedIndexChanged">
        <asp:ListItem Value="15">15 Years</asp:ListItem>
        <asp:ListItem Value="30">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;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtYears" runat="server" Enabled="False"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Please select the interest rate"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="ddlInterestRate" 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" onclick="Button1_Click" 
        Text="Monthly Payment" />
    <br />
    <br />
    <asp:Label ID="Label4" runat="server"></asp:Label>
    </form>
</body>
</html>
最后,我当前的aspx.cs代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    //double principle = 0;
    //double years = 0;
    double interest = 0;
    //string principalInput, yearsInput, interestInput;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtYears.Enabled = false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        double principle = 0.0;
        double principleInput = double.Parse(txtPrinciple.Text);
        double years = double.Parse(txtYears.Text);
        var yearDuration = rblYears.SelectedValue;
        double interestInput = double.Parse(ddlInterestRate.SelectedValue);
        if (double.TryParse(txtPrinciple.Text, out principle))
        {
        }
    }
    protected void rblYears_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rblYears.SelectedIndex == 3)
            txtYears.Enabled = true;
        else
        {
            txtYears.Text = "";
            txtYears.Enabled = false;
        }
    }
    protected void txtPrinciple_TextChanged(object sender, EventArgs e)
    {
       // principle = double.Parse(txtPrinciple.Text);
    }
}

我知道这是不完整的,我只是遇到了一些障碍,我想知道我是否走在正确的方向上。如果有人能引导我正确的方式,我将不胜感激。谢谢大家。

控制台应用程序在c#到ASP.净的困难

我倾向于将您的LoanCalculator类引入您的ASP。网络站点。

如果它是一个网站,而不是Web应用程序项目,然后把它放在你的App_Code文件夹。否则,你可以把它放在任何你喜欢的地方。

稍微修改一下:

删除using System.Threading.Tasks; -这里不需要。

同时,而不是public static void Main(string[] args)

改为

public static double MonthlyPayment(string[] args)

删除所有的Console.Write代码,并简单地return monthlyPayment;后,你已经计算。

你可以从你的页面代码调用它,只需调用:

myPaymentLabel.Text = String.Format("{0:C}",LoanCalculator.LoanCalculator.MonthlyPayment(yourArgs));

String.Format()部分将通过传递格式{0:C}将结果转换为货币十进制。

这将是使您的LoanCalculator工作所涉及的基本知识。

当然,您可以进一步扩展它,例如赋予类Public Properties并将public static decimal MonthlyPayment(string[] args)更改为Public LoanCalculator(string[] args) -然后用您的结果填充类的属性。