0% found this document useful (0 votes)
3 views

pract-8

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

pract-8

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FACULTY OF ENGINEERING AND TECHNOLOGY

Department of Computer Engineering


01CE0523 - .NET Technologies

Experiment 8

AIM: Creating web application using MVC

Code :

CurrencyConverterController

using Prcatical_8._3.Models;
using System;
using System.Collections.Generic;
using System.Įinq;
using System.Web;
using
System.Web.Mvc;

namespace Prcatical_8._3.Controllers
{
public class CurrencyConverterController : Controller
{

public ActionResult Index()


{
return View();
}

public ActionResult CurrencyConverter()


{
var model = new CurrencyConverterModel
{
FromCurrency = "I
R", ToCurrency =
"JPY",
CurrencySelectĮist = CurrencyConverterModel.GetCurrencies()
};
return View(model);
}

// store conversion.
private static readonly Dictionary<string, decimal> exchangeRates =
new Dictionary<string, decimal>
{
{ "I R", 1.0m }, // Base rate for I R (reference)
{ "JPY", 1.5m }, // 1 I R = 1.5 JPY
{ "GBP", 0.010m }, // 1 I R = 0.010 GBP
{ "RUB", 0.83m } // 1 I R = 0.83 RUB
};

public ActionResult Convert(CurrencyConverterModel model)

YASH ANAND (92201703234) BATCH: A |21


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0523 - .NET Technologies
{

YASH ANAND (92201703234) BATCH: A |22


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0523 - .NET Technologies

if (exchangeRates.ContainsKey(model.FromCurrency) fifi
exchangeRates.ContainsKey(model.ToCurrency))
{
decimal fromRate = exchangeRates[model.FromCurrency];
decimal toRate = exchangeRates[model.ToCurrency];

// Conversion formula: (Amount / FromRate) * ToRate


model.ConvertedAmount = (model.Amount / fromRate) * toRate;
}

// Repopulate the CurrencySelectĮist for the dropdown lists in the


view. model.CurrencySelectĮist =
CurrencyConverterModel.GetCurrencies(); return
View("CurrencyConverter", model);
}

}
}

CurrencyConverterModel

using System;
using System.Collections.Generic;
using System.Įinq;
using System.Web;
using
System.Web.Mvc;

namespace Prcatical_8._3.Models
{
public class CurrencyConverterModel
{

public string FromCurrency { get; set;


} public string ToCurrency { get; set;
} public decimal Amount { get; set; }
public decimal ConvertedAmount { get; set; }

//SelectĮistItem
public Įist<SelectĮistItem> CurrencySelectĮist { get; set; }

// currency list as SelectĮistItem


public static Įist<SelectĮistItem> GetCurrencies()
{
return new Įist<SelectĮistItem>
{
new SelectĮistItem { Value = "I R", Text = "Indian
Rupee" }, new SelectĮistItem { Value = "JPY", Text =
"Japanese Yen" }, new SelectĮistItem { Value = "GBP",
Text = "British Pound" }, new SelectĮistItem { Value =
"RUB", Text = "Russian Ruble" }
};
}
}
}

YASH ANAND (92201703234) BATCH: A |23


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0523 - .NET Technologies

CurrencyConverterView

@model Prcatical_8._3.Models.CurrencyConverterModel

@{
ViewBag.Title = "CurrencyConverter";
}

<h2>CurrencyConverter</h2>

@using ( tml.BeginForm("Convert", "CurrencyConverter", FormMethod.Post))


{
<div style="display:flex">
<div style="margin-right:30px">
<label for="FromCurrency">From:</label>
@ tml.DropDownĮistFor(model => model.FromCurrency,
Model.CurrencySelectĮist, "Select a currency", new { @class = "form-control"
})
</div>

<div>
<label for="ToCurrency">To:</label>
@ tml.DropDownĮistFor(model => model.ToCurrency,
Model.CurrencySelectĮist, "Select a currency", new { @class = "form-control"
})
</div>

</div>

<div style="margin-block:20px">
<label for="Amount">Amount:</label>
@ tml.TextBoxFor(model => model.Amount, new { @class = "form-
control", type = "number", step = "0.01", placeholder = "Enter amount" })

</div>

<div>
<input type="submit" value="Convert" class="btn btn-primary" />

</div>

@if (Model.ConvertedAmount > 0)


{

<div style="margin-block:20px">
<h2>Converted Amount: @Model.ConvertedAmount</h2>
</div>

YASH ANAND (92201703234) BATCH: A |24


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0523 - .NET Technologies

Screenshot:

YASH ANAND (92201703234) BATCH: A |25

You might also like