Professional Documents
Culture Documents
MVC Practice02
MVC Practice02
NET MVC
MVC_Practice02
េ លបំណងរបស់ MVC_Practice02 គេឺ បី ស់ ASP.NET MVC សំ ប់េធី រ រដូច ងេ ម
- េ បី ស់ Razor Syntax េ កុ ង MVC View
- បេងត
ី Model ស ប់េ ះ យប េផ ងៗ
- បេងត
ី Controller េដីម ី Handle User Request និង Response
- បេងត
ី View ស ប់ឲ អកេ បី ស់ ប ូ លទិនន័យេផ ងៗ
- ែកែ ប Layout MVC Project េដម ី ំណត់ទ មងៃ់ ន
ី ក រប ញ
សូមបូ នៗ ង
ំ អស់ ចូលរួមអនុវត ជំ ៊ នៗដូច ងេ ម
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); }
}
}
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;
}
}
}
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) + " -- ";
}
return str;
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();
}
[HttpPost]
public ActionResult Ex01(Rectangle rectangle)
{
return View(rectangle);
}
[HttpPost]
public ActionResult Ex02(Multiplication multiplication)
{
return View(multiplication);
}
[HttpPost]
public ActionResult Ex03(ShowRandom showRandom)
{
return View(showRandom);
}
}
}
@{
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> |
<a href="/RazorExample/Ex02" class="btn btn-warning btn-lg">Multiplication</a> |
<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> |
<a href="#" class="btn btn-default btn-lg">Task02</a> |
<a href="#" class="btn btn-info btn-lg">Task03</a>
</p>
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;
}
@{
var Width = Model != null ? Model.Width.ToString() : "";
var Length = Model != null ? Model.Length.ToString() : "";
}
<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" />
<input type="reset" value="Reset" name="btnreset" />
</td>
</tr>
</table>
</form>
3- បេងត
View េ ះ Ex02.cshtml េ កុ ង RazorExample Foleder ដូច ងេ ម
@model MVC_Practice02.Models.Multiplication
@{
ViewBag.Title = "Ex02";
}
@{
var Number = Model != null ? Model.Number.ToString() : "";
}
<h2>Multiplication</h2>
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() : "";
}
<tr style="text-align:center;">
<td colspan="2">
<input type="submit" value="All Number" name="btnall" />
<input type="submit" value="Odd Number" name="btnodd" />
</td>
</tr>
</table>
</form>
<br />
if (!Request["btnodd"].IsEmpty())
{
<p>Odd Numbers : @Html.Raw(Model.OddRandom())</p>
}
}
លទផលដូច ងេ ម
Task01
Task02
Task03