You are on page 1of 19

ASP.

NET MVC

MVC_Practice02
េ លបំណងរបស់ MVC_Practice02 គេឺ បី ស់ ASP.NET MVC សំ ប់េធី រ រដូច ងេ ម
- េ បី ស់ Razor Syntax េ កុ ង MVC View
- បេងត
ី Model ស ប់េ ះ យប េផ ងៗ
- បេងត
ី Controller េដីម ី Handle User Request និង Response
- បេងត
ី View ស ប់ឲ អកេ បី ស់ ប ូ លទិនន័យេផ ងៗ
- ែកែ ប Layout MVC Project េដម ី ំណត់ទ មងៃ់ ន
ី ក រប ញ
 សូមបូ នៗ ង
ំ អស់ ចូលរួមអនុវត ជំ ៊ នៗដូច ងេ ម

LECTURER : MR. HENG BORA 1


ASP.NET MVC

I- បេងីត MVC Project


បេងត
ី MVC Project : MVC_Practice02 (យកលំ ំ ម MVC_Practice01)

LECTURER : MR. HENG BORA 2


ASP.NET MVC

II- Model
1. បេងត
 Model េ ះ Rectangle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC_Practice02.Models
{
public class Rectangle
{
//Properties
public double Width { get; set; }
public double Length { get; set; }

//Methods
public double Area() { return Width * Length; }
public double Perimter() { return 2 * (Width + Length); }
}
}

LECTURER : MR. HENG BORA 3


ASP.NET MVC

2. បេងត
 Model េ ះ Multiplication

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC_Practice02.Models
{
public class Multiplication
{
//Properties
public int Number { get; set; }

//Method
public String Display()
{
String str = "";
for(var i = 1; i <= 10; i++){
str += "<p>" + Number + " x " + i + " = " + i*Number + "</p>";
}
return str;

}
}
}

LECTURER : MR. HENG BORA 4


ASP.NET MVC

3. បេងត
 Model េ ះ ShowRandom

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC_Practice02.Models
{
public class ShowRandom
{
//Properties
public int Min { set; get; }
public int Max { set; get; }
public int Num { set; get; }

//Method
public string AllRandom()
{
Random objR = new Random();
string str = "";
for (int i = 0; i < Num; i++)
{
str += objR.Next(Min, Max) + "&nbsp;--&nbsp;";
}
return str;

LECTURER : MR. HENG BORA 5


ASP.NET MVC

public string OddRandom()


{
Random objR = new Random();
string str = "";
int r;
int count = 0;
while(true)
{
r = objR.Next(Min, Max);
if (r % 2 != 0) {
str += r + "&nbsp;--&nbsp;";
count++;
}
if (count == Num) return str;
}
return str;
}
}
}

LECTURER : MR. HENG BORA 6


ASP.NET MVC

III- បេងតMVC5-Empty Controller េ ះ RazorExampleController ដូច ងេ ម

using MVC_Practice02.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_Practice02.Controllers
{
public class RazorExampleController : Controller
{
// GET: RazorExample
public ActionResult Index()
{
return View();
}

public ActionResult Ex01()


{
return View();
}

[HttpPost]
public ActionResult Ex01(Rectangle rectangle)
{
return View(rectangle);
}

LECTURER : MR. HENG BORA 7


ASP.NET MVC

public ActionResult Ex02()


{
return View();
}

[HttpPost]
public ActionResult Ex02(Multiplication multiplication)
{
return View(multiplication);
}

public ActionResult Ex03()


{
return View();
}

[HttpPost]
public ActionResult Ex03(ShowRandom showRandom)
{
return View(showRandom);
}
}
}

LECTURER : MR. HENG BORA 8


ASP.NET MVC

IV- បេងត View


1- បេងត
 View េ ះ Index.cshtml េ កុ ង RazorExample Foleder ដូច ងេ ម

@{
ViewBag.Title = "Index";
}
<style>
p a {
width:250px;
}
</style>
<h2>MVC Razor and Model</h2>
<br />
<p>
<a href="/RazorExample/Ex01" class="btn btn-primary btn-lg">Rectangle</a> &nbsp;| &nbsp;
<a href="/RazorExample/Ex02" class="btn btn-warning btn-lg">Multiplication</a> &nbsp;|
&nbsp;
<a href="/RazorExample/Ex03" class="btn btn-success btn-lg">RandomNum</a>
</p>
<hr style="border:5px solid #ff6a00;" />
<h2>All Your Tasks</h2>
<p>
<a href="#" class="btn btn-danger btn-lg">Task01</a> &nbsp;| &nbsp;
<a href="#" class="btn btn-default btn-lg">Task02</a> &nbsp;| &nbsp;
<a href="#" class="btn btn-info btn-lg">Task03</a>
</p>

LECTURER : MR. HENG BORA 9


ASP.NET MVC

2- បេងត
 View េ ះ Ex01.cshtml េ កុ ង RazorExample Foleder ដូច ងេ ម

@model MVC_Practice02.Models.Rectangle

@{
ViewBag.Title = "Ex01";
}
<style>
input[type=text] {
width: 100%;
padding: 8px 20px;
margin: 8px 0;
box-sizing: border-box;
}

input[type=button], input[type=submit], input[type=reset] {


background-color: #4CAF50;
border: none;
color: white;
padding: 10px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
</style>

@{
var Width = Model != null ? Model.Width.ToString() : "";
var Length = Model != null ? Model.Length.ToString() : "";
}

LECTURER : MR. HENG BORA 10


ASP.NET MVC

<h2>Rectangle</h2>
<hr style="border:5px solid #ff6a00;" />
<form action="/RazorExample/Ex01" method="post">
<table>
<tr>
<td>Width : </td>
<td><input type="text" name="Width" value="@Width" /> </td>
</tr>
<tr>
<td>Length : </td>
<td><input type="text" name="Length" value="@Length" /> </td>
</tr>
<tr style="text-align:center;">
<td colspan="2">
<input type="submit" value="OK" name="btnok" /> &nbsp;
<input type="reset" value="Reset" name="btnreset" />
</td>
</tr>
</table>
</form>

@if (Model != null)


{
<p>Area : @Model.Area()</p>
<p>Perimter : @Model.Perimter()</p>
}

LECTURER : MR. HENG BORA 11


ASP.NET MVC

3- បេងត
 View េ ះ Ex02.cshtml េ កុ ង RazorExample Foleder ដូច ងេ ម

@model MVC_Practice02.Models.Multiplication

@{
ViewBag.Title = "Ex02";
}

@{
var Number = Model != null ? Model.Number.ToString() : "";
}
<h2>Multiplication</h2>

<form action="/RazorExample/Ex02" method="post">


<p>
Number : <input type="text" name="Number" value="@Number" />
<input type="submit" value="OK" name="btnok" /> &nbsp;
</p>
</form>

<hr style="border:5px solid #4cff00;" />


@if (Model != null)
{
<p>@Html.Raw(@Model.Display())</p>
}

LECTURER : MR. HENG BORA 12


ASP.NET MVC

4- បេងត
 View េ ះ Ex03.cshtml េ កុ ង RazorExample Foleder ដូច ងេ ម

@model MVC_Practice02.Models.ShowRandom

@{
ViewBag.Title = "Ex03";
}

@{
var Min = Model != null ? Model.Min.ToString() : "";
var Max = Model != null ? Model.Max.ToString() : "";
var Num = Model != null ? Model.Num.ToString() : "";
}

<h2>Generate Random Number</h2>


<hr style="border:5px solid #ff0000;" />
<form action="/RazorExample/Ex03" method="post">
<table>
<tr>
<td>Min : </td>
<td><input type="text" name="Min" value="@Min" /> </td>
</tr>
<tr>
<td>Max : </td>
<td><input type="text" name="Max" value="@Max" /> </td>
</tr>
<tr>
<td>Num : </td>
<td><input type="text" name="Num" value="@Num" /> </td>
</tr>

LECTURER : MR. HENG BORA 13


ASP.NET MVC

<tr style="text-align:center;">
<td colspan="2">
<input type="submit" value="All Number" name="btnall" /> &nbsp;
<input type="submit" value="Odd Number" name="btnodd" />
</td>
</tr>
</table>
</form>
<br />

@if (Model != null)


{
if (!Request["btnall"].IsEmpty())
{
<p>All Numbers : @Html.Raw(Model.AllRandom())</p>
}

if (!Request["btnodd"].IsEmpty())
{
<p>Odd Numbers : @Html.Raw(Model.OddRandom())</p>
}
}

LECTURER : MR. HENG BORA 14


ASP.NET MVC

V- ែកែ ប Layout MVC Project

LECTURER : MR. HENG BORA 15


ASP.NET MVC

លទផលដូច ងេ ម

LECTURER : MR. HENG BORA 16


ASP.NET MVC

VI- កិច របែនម

Task01

LECTURER : MR. HENG BORA 17


ASP.NET MVC

Task02

LECTURER : MR. HENG BORA 18


ASP.NET MVC

Task03

LECTURER : MR. HENG BORA 19

You might also like