Example of TextboxFor in ASP.NET MVC TextBox Control
This example demonstrates strongly typed helper of TextBox. Edit the TextBox's value and click on 'Submit' button to pass the modified value to controller.
@Html.UiTextBoxFor(model => model.firstname, new UiTextBoxOptions { Label = "First Name", FloatLabelType = UiFloatLabelType.Auto })
@Html.UiButton(new UiButtonOptions { Id = "btn", Text = "Post", Type = UiButtonType.Submit, Color = UiButtonColor.Primary })
<style>
#wrapper { max-width: 246px; margin: 30px auto; padding-top: 50px; }
#submitbutton { margin-top: 20px; margin-left: 65px; }
#wrapper .field-validation-error { color: red; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
@model InputsDemoController.TextBoxModal
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="control-section">
<div id="wrapper">
@Html.UiTextBoxFor(model => model.firstname, new UiTextBoxOptions { Label = "First Name", FloatLabelType = UiFloatLabelType.Auto })
<div>
@Html.ValidationMessageFor(model => model.firstname)
</div>
<div id="submitbutton">
@Html.UiButton(new UiButtonOptions { Id = "btn", Text = "Post", Type = UiButtonType.Submit, Color = UiButtonColor.Primary })
</div>
</div>
</div>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TextBoxController : Controller
{
// The page is Views/TextBox/TextboxFor.cshtml; it draws what ViewData holds.
public ActionResult TextboxFor()
{
textbox.firstname = "John";
return View(textbox);
}
[HttpPost]
public ActionResult TextboxFor(TextBoxModal model)
{
// the posted value is obtained from the model
return View(model);
}
public class TextBoxModal
{
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Value is required")]
public string firstname { get; set; }
}
private readonly TextBoxModal textbox = new TextBoxModal();
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
In this sample, the TextBox is rendered with strongly typed helper(FOR control) and bind its value from Controller using model binding. The modified value is bind to controller action "TextboxFor" when submit a page.