MVC Calculator App: in This Workshop We Will Create A Web Calculator App
MVC Calculator App: in This Workshop We Will Create A Web Calculator App
MVC Calculator App: in This Workshop We Will Create A Web Calculator App
5.Open the solution, right click on the controllers and add a controller,
name it “HelloWorldController”.
Note that the name of the controller must end with the controller key
word or else it won’t be recognized as a controller:
9. Change the default behavior such that the Welcome action is the
default method. Test it (you must restart the app):
/[Controller]/[ActionName]/[Parameters]
13. Adding the view : Right click inside the index method and select add
view:
The layout is the HTML container file .By default all the views that we
create are appended to it. Open the file _Layout.cshtm:
To:
<p class="site-title">@Html.ActionLink("Web Calculator", "Index",
"Home")</p>
Replace the title with:
@{
ViewBag.Title = "Welcome";
}
<h2>Welcome</h2>
<ul>
@for (int i=0; i < ViewBag.NumTimes; i++) {
<li>@ViewBag.Message</li>
}
</ul>
Run the application and browse to the following URL:
http://localhost:xx/HelloWorld/Welcome?name=yourname&numtimes=10
Now data is taken from the URL and passed to the controller using the
model binder. The controller packages the data into a ViewBag object and
passes that object to the view. The view then displays the data as HTML to
the user.
The BL layer: Adding a model
We’ll create a class that perform the 4 basic arithmetic
operations:
Increment, Subtract, Multiply and Divide.
18. click on the model folder and Add a new class. Call it
calculator:
22. Copy paste the following to the new view we’ve just
created:
int x, y, result;
ViewBag.x = x;
ViewBag.y=y;
ViewBag.result = result;
return View("Index");
}