Professional Documents
Culture Documents
It Is Intimated That Following Lectures Will Not Be Repeated and Would Be Part of Mid-Term& Final Exam As Well
It Is Intimated That Following Lectures Will Not Be Repeated and Would Be Part of Mid-Term& Final Exam As Well
Note:
It is intimated that following Lectures will not be repeated and would be part
of mid-term& final exam as well.
Understanding Forms
Form Action and Method
Html Helpers
Post and Get Requests in Controllers
Arithmetic Operations
Text Book & Resources: Professional ASP.NET MVC 5 by Jon Galloway, Brad Wilson, K. Scott Allen,
David Matson
Video Links:
https://www.youtube.com/watch?v=qfVPwPtFt-A&list=PLjVp2I_4DPy_4CvU-
mdB_vfHfT8k7o18N&index=5&t=202s
https://www.youtube.com/watch?v=Zrd38uzld2o&list=PLjVp2I_4DPy_4CvU-
mdB_vfHfT8k7o18N&index=6&t=83s
HTML Form
A form is a container for input elements: buttons, checkboxes, text inputs, and more. The input elements
in a form enable a user to enter information into a page and submit information to a server. The two most
important attributes of a form tag: the action and the method attributes.
<form action="http://www.bing.com/search">
<input name="q" type="text" />
<input type="submit" value="Search!" />
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
</form>
animal videos on YouTube. However, the default method value is “Get,” so by default a form sends an
HTTP GET request:
When a user submits a form using an HTTP GET request, the browser takes the input names and values
inside the form and puts them in the query string. In other words, the preceding form would send the
browser to the following URL (assuming the user is searching for data):
http://www.bing .com/search?q=data
A POST, on the other hand, is the type of request you use to submit a credit card transaction, add an
album to a shopping cart, or change a password. A POST request generally modifies state on the server,
and repeating the request might produce undesirable effects (such as double billing). Many browsers help
a user avoid repeating a POST request.
HTML HELPERS
ASP.NET MVC provides Html Helper class which contains different methods that help you create HTML
controls programmatically. All Html Helper methods generate HTML and return the result as a string.
The final HTML is generated at runtime by these functions. The Html Helper class is designed to generate
UI and it should not be used in controllers or models.
ActionMethod – It defines which action method is look for when submit button is clicked.
ControllerName – It defines, which controller keeps the defined action method.
Get/Post Method – it defines the method you want to use to send data from form/view to controller.
Notice that the Html.BeginForm() method is called within a using statement. The using statement ensures
that the <form> tag gets closed at the end of the using block.
All from element input fields including submit button must be within the given block curly braces of
beginForm.
Example 1:
Each form contains three basic elements: a <form> element, <input> elements (which the user can type
to), and some type of a submit button.
In this example we are going to use almost all commonly used html helpers for input.
Create a new MVC Project, go to Files - > New Project -> ASP.NET Web Application and Select MVC
Template. Give a name and Click on Ok.
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
Controllers: HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCApp.Controllers
{
public class HomeController : Controller
{
// GET: /
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
Views: Index.cshtml
That is the main container, we will place all controls inside the tag.
Here in Html.Beginform we are passing View name (Index), Controller name (Home) and declare
formMethod = Post Method.
If you are not passing any parameter like View name and Controller name then it will automatically call
the current controller action method having [HttpPost] attribute.
This will create a Lable Named UserName, so for Creating Label we will use @Html.Lable
Next, We need Textbox for the UserName, for that we use Html.TextBox same
as @Html.TextBox("UserName").
@Html.Label("UserName")
@Html.TextBox("UserName")
<br />
}
Same way we will design UserPhone, UserEmail Text Fields etc.
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<br />
@Html.Label("UserName")
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
@Html.TextBox("UserName")
<br />
@Html.Label("UserPhone")
@Html.TextBox("UserPhone")
<br />
@Html.Label("UserEmail")
@Html.TextBox("UserEmail")
<br />
}
@Html.Label("UserPassword")
@Html.Password("UserPassword")
<br />
For Gender (Male/Female) we can use RadioButton and for that you can use Html.RadioButton as
follows
@Html.Label("M")
@Html.RadioButton("M", new { value = "Male" })
@Html.Label("F")
@Html.RadioButton("F", new { value = "Female" })
<br />
For Country selection we can use dropdown and for that we have Html.DropDownList.
Here we have to create a list for the country for that I have added below code outside of BeginForm tag.
@{
//C# code to generate list for dropdown
};
}
Now Use this MyList in your Html.DropDownList as follows.
@Html.Label("UserCountry")
@Html.DropDownList("UserCountry", MyList)
@*
passing list “My List” to populate drop down with UK and Pak value..
Note: This is style to comment in Razor Code!
*@
<br />
Same way for Checkbox we use Html.CheckBox
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
<br />
@Html.Label("UserTerms")
@Html.CheckBox("UserTerms")
<br />
Now run your application. It will display your html helper contols like label, textbox, rediobutton, etc.
Example 2:
Create a Form with Simple HTML Helper Class and Arithmetic Operation
Step 1: Controllers
Create an ActionResult in controller class
[HttpGet]
public ActionResult NewFormView()
{
return View();
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
}
[HttpPost]
public ActionResult NewFormView(int n1tb, int n2tb)
{
<h1>New Calculator</h1>
@using (Html.BeginForm())
{
<b>Num 1:</b>@Html.TextBox("n1tb")<br />
<b>Num 2:</b>@Html.TextBox("n2tb")<br />
<input type="submit" value="Add"/>
}
<b>Result:</b>@ViewBag.Result
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
Activity 1:
Create an ASP.NET MVC Web Application that can perfom a ArithmeticOperation.
[HttpGet]
public ActionResult DDLView()
{
ViewBag.Operations = GetItems();
return View();
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
}
Step 4:Add a view against DDLView action. Set Template and model as empty as we don’t have
any model in our project.
<h2>Arithmetic Operations</h2>
@using (Html.BeginForm())
{
<b>Num1</b>@Html.TextBox("n1tb") <br />
<b>Num2</b>@Html.TextBox("n2tb") <br />
<b>Operation</b>@Html.DropDownList("opt",(List<SelectListItem>)ViewBag.Operations,"S
elect")
<input type="submit" value="submit" />
}
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
<b>Rsult</b>@ViewBag.Result
Step 5: Create a [HttpPost] Action with the same name as DDLView
[HttpPost]
int res = 0;
switch (opt)
case "Add":
break;
case "Sub":
break;
default:
res = 0;
break;
ViewBag.Result = res;
ViewBag.Operations = GetItems();
return View(); }
Step 6: Add a Method in Controller which returns the list of item present in the DropDownList.
private List<SelectListItem>GetItems()
return items;
Controller Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ArithmeticOperationMVCApp.Controllers
{
//Start Controller Code
public class ArithmeticController : Controller
{
//
// GET: /Arithmetic/
[HttpPost]
public ActionResult DDLView(int n1tb, int n2tb, string opt)
{
int res = 0;
switch (opt)
{
case "Add":
res = n1tb + n2tb;
break;
case "Sub":
res = n1tb - n2tb;
break;
default:
res = 0;
break;
}
ViewBag.Result = res; // adding res into ViewBag to access res on view
return View();
}
private List<SelectListItem> GetItems()
{
//List to populate drop down on view
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem{Text="Add", Value="Add"});
View Code:
<h2>Arithmetic Operations</h2>
@using (Html.BeginForm())
<b>Operation</b>@Html.DropDownList("opt",(List<SelectListItem>)ViewBag.Operations,"Select")
<b>Result:</b>@ViewBag.
Web Engineering (CS-666/CS-723)
Mr. Aftab Khan email id: aftab@biit.edu.pk Whatsapp# 0344-8184704
Mr. Amir Rashid email id: amir@biit.edu.pk, Whatsapp# 0302-5698539
Assignment #2
Dear students read the given lectures carefully as per the course objectives mentioned on the top
and carryout the assignment as per following instructions
1. Submission Date: Sunday 22-March-2020 at 11:59 PM ; This will also count as your
Attendance for this week.
2. You must prepare handwritten Assignment with implementation in the form of project.
3. Send it to respective course teacher (after scanning it) for assessment by email only.
Percentage Calculator: