- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using ViewBagDemoApp.Models;
- namespace ViewBagDemoApp.Controllers
- {
- public class AdditionController : Controller
- {
-
- public ActionResult Index(Addition addition)
- {
- ViewBag.AddMessage = "Addition Result is: ";
- ViewBag.Result = addition.FirstNumber + addition.SecondNumber;
- if (ViewBag.Result < 1)
- {
- ViewBag.AddMessage = string.Empty;
- ViewBag.Result = string.Empty;
- }
- return View();
- }
- }
- }
Step 4: Adding new View

- @model ViewBagDemoApp.Models.Addition
- @{
- ViewBag.Title = "Addition Operation Using ViewBag";
- }
- <h2>Addition Operation Using ViewBag</h2>
- <br />
- @using (Html.BeginForm())
- {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>Addition</legend>
- <div class="editor-label">
- @Html.LabelFor(model => model.FirstNumber)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.FirstNumber)
- @Html.ValidationMessageFor(model => model.FirstNumber)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.SecondNumber)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.SecondNumber)
- @Html.ValidationMessageFor(model => model.SecondNumber)
- </div>
- <p>
- @ViewBag.AddMessage @ViewBag.Result
- </p>
- <br />
- <p>
- <input type="submit" value="Add" />
- </p>
- </fieldset>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
Step 5: The output for the application is as in the following


I hope this article was useful for you. I look forward to your comments and feedback. Thanks, Vijay.
Gowtham RajamanickamPosted Mar 11, 2015, 8:33 AM
good....