This is very good article for web form users who want to get value on TextChanged Event. I mean to say that TextChanged Event occurs when we move one textbox to another using "Tab" key. When, Web form users moves to MVC projects. He/ She faces some problems in logics. Because, in MVC, For each request we must to call controller. I have an idea to remove such task. I prefer JQuery. So first to Add a ViewModel Class into your project. You can take this sample.
1. Add a new Folder in your project, name of the folder is "viewModel". Now, add a class, which name as addition like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication27.viewModel
{
public class addition
{
public int FirstNumer { get; set; }
public int SecondNumber { get; set; }
public int result { get; set; }
}
}
In this we have three public property.Now, Add a controller class in Controller folder. Like that.
using System.Web.Mvc;
namespace WebApplication27.Controllers
{
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
return View();
}
}
}
Add View, By using Right click on Action method name. In View section, i will add code of JQuery. You can see
@model WebApplication27.viewModel.addition
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using(Html.BeginForm())
{
@Html.TextBoxFor(m=>Model.FirstNumer)
@Html.TextBoxFor(m=>Model.SecondNumber)
@Html.TextBoxFor(m => Model.result)
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(function () {
$("#SecondNumber").blur(function () {
$("#result").val(parseInt($("#FirstNumer").val()) + parseInt($("#SecondNumber").val()))
});
})
</script>
1. Add a new Folder in your project, name of the folder is "viewModel". Now, add a class, which name as addition like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication27.viewModel
{
public class addition
{
public int FirstNumer { get; set; }
public int SecondNumber { get; set; }
public int result { get; set; }
}
}
In this we have three public property.Now, Add a controller class in Controller folder. Like that.
using System.Web.Mvc;
namespace WebApplication27.Controllers
{
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
return View();
}
}
}
Add View, By using Right click on Action method name. In View section, i will add code of JQuery. You can see
@model WebApplication27.viewModel.addition
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using(Html.BeginForm())
{
@Html.TextBoxFor(m=>Model.FirstNumer)
@Html.TextBoxFor(m=>Model.SecondNumber)
@Html.TextBoxFor(m => Model.result)
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(function () {
$("#SecondNumber").blur(function () {
$("#result").val(parseInt($("#FirstNumer").val()) + parseInt($("#SecondNumber").val()))
});
})
</script>