Adding HTML attributes to HTML helpers (controls) in ASP.NET MVC

by Jagadish Pulakhandam on 6/14/2012 7:21:43 PM
Rated 0 from 0 votes
Brief: Demonstrates on adding HTML attributes to ASP.NET MVC controls (HTML helpers) in ASP.NET MVC markup
Posted to: Basic Tutorials/samples on ASP.NET MVC Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached source code demonstrates the following:
  • Rows and Columns using DIV (and not using TABLE and related tags)
  • Using Textbox in ASP.NET MVC
  • Providing more HTML attributes to TextBox control 
  • Adding "Style" and "MaxLength" attributes to ASP.NET MVC TextBox control
  • Sharing data using "ViewData" between View (markup) and Controller
Screenshot:



Source Code:
 
Model:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05. 
06.namespace MvcApplication1.Models
07.{
08.    public class SimpleMathInput
09.    {
10.        public int? FirstNo { get; set; }
11.        public int? SecondNo { get; set; }
12.    }
13.}

Controller:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05.using System.Web.Mvc;
06.using MvcApplication1.Models;
07. 
08.namespace MvcApplication1.Controllers
09.{
10.    public class SimpleMathController : Controller
11.    {
12.        //
13.        // GET: /SimpleMath/
14. 
15.        public ActionResult Index()
16.        {
17.            return View();
18.        }
19. 
20. 
21.        [AcceptVerbs(HttpVerbs.Post)]
22.        public ActionResult Index(SimpleMathInput o)
23.        {
24.            string result = string.Empty;
25. 
26.            if (o.FirstNo.HasValue && o.SecondNo.HasValue)
27.            {
28.                result = string.Format("Sum = {0}", o.FirstNo.Value + o.SecondNo.Value);
29.            }
30. 
31.            ViewData["result"] = result;
32. 
33.            return View(o);
34.        }
35. 
36.    }
37.}

View:

01.<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.SimpleMathInput>" %>
02. 
03.<!DOCTYPE html>
04.<html>
05.<head runat="server">
06.    <title>Index</title>
07.    <style type="text/css">
08.        .cell
09.        {
10.            width: 150px;
11.            padding: 5px 5px 5px 5px;
12.            float: left;
13.        }
14.    </style>
15.</head>
16.<body>
17.    <div>
18.        <% using (Html.BeginForm())
19.           { %>
20.        <div class="cell">
21.            Enter First No:
22.        </div>
23.        <div class="cell">
24.            <%= Html.TextBox("FirstNo", null, new { style = "width: 100px", maxlength="4" }) %>
25.        </div>
26.        <div class="cell" style="clear: both">
27.            Enter Second No:
28.        </div>
29.        <div class="cell">
30.            <%= Html.TextBox("SecondNo", null, new { style = "width: 100px", maxlength="4" }) %>
31.        </div>
32.        <div class="cell" style="clear: both">
33.            <input type="submit" />
34.        </div>
35.        <div class="cell" style="clear: both">
36.            <%= ViewData["result"] %>
37.        </div>
38.        <%} %>
39.    </div>
40.</body>
41.</html>

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following