ASP.NET Feature

1. 404 Handling

Controller extend MyController

Contoh kode:

[code language=”csharp”]
var wrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
this.InvokeHttp404(wrapper);
return null;
[/code]

2. Custom Validation

Class untuk custom validation disimpan di folder WebUI/Infrastructure/Validation

Fungsi custom validation yang ada:

[code language=”csharp”]
[CheckDay(Day.SUNDAY)] //input hari harus hari minggu
[DateGreater("StartDate")] //input tanggal harus lebih besar dari model.StartDate
[/code]

 3. Notification

Menampilkan pesan untuk user, misalnya pesan sukses setelah user menambah/mengubah suatu data.

Contoh kode

[code language=”csharp”]
//…
using WebUI.Extension; //wajib ditambahkan

namespace WebUI.Controllers
{
public class FidItemController : MyController
{
//…

[HttpPost]
public ActionResult Create(FidItemFormStub model)
{
if (ModelState.IsValid)
{
//…

//message
string template = HttpContext.GetGlobalResourceObject("MyGlobalMessage", "CreateSuccess").ToString();
this.SetMessage(model.Title, template);

return RedirectToAction("Index", new { projectId = model.ProjectId });
}
}

//…
}
}

[/code]

Template pesan disimpan di file WebUI/App_GlobalResources/MyGlobalMessage.resx. Silakan ditambah/diubah seperlunya.

Contoh kode di view

[code language=”csharp”]
var currentController = (WebUI.Controllers.ProjectController)ViewContext.Controller;
@Html.Raw(currentController.GetMessage());
[/code]

4. Form Editor

Template editor disimpan di folder WebUI/Views/Shared/EditorTemplates.

Contoh kode

[code language=”csharp”]
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
[/code]

Penanganan khusus untuk menampilkan radiobutton list dari Enum.

[code language=”csharp”]
//Enum
public enum IncidentReportLevel
{
[Description("Major")]
MAJOR,
[Description("Serious")]
SERIOUS,
[Description("Moderate")]
MODERATE,
[Description("Minor")]
MINOR
}

//View
@Html.EditorFor(model => model.PotentialLossSeverity, "EnumRadioButtonList", new { cssClass = "col-xs-3" })
[/code]

Hasil

Capture

Chandra Oemaryadi has written 244 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>