I have been searching for this for a while now but I could not find anything that solved my problem.
Problem:
I have been given a task to show sum of two numbers on the run, For Example:
TextBox1 = 2
TextBox2 = 3
Result = 5
Now as soon as the user changes any of the TextBox1
or TextBox2
value the Result
should display their sum immediately and I have to do this using AngularJs
.
What have I done so far:
I started learning AngularJs
and I think adding two numbers is very simple as described above, My code below can do it:
<div data-ng-app="" data-ng-init="numberA=2;numberB=3">
Number 1: <input type="number" ng-model="numberA">
Number 2: <input type="number" ng-model="numberB">
<p><b>Sum:</b> {{numberA + numberB}}</p>
</div>
What I can not do is, do the exact same thing in ASP.NET MVC 4, I created a new Project and the above worked fine but when I started using the Models
it does not work.
Model:
public class Check
{
public int numberA { get; set; }
public int numberB { get; set; }
}
View:
<div data-ng-app="">
@Html.TextBoxFor(model => model.numberA, new { @data-ng-model = "numberA" })
@Html.TextBoxFor(model => model.numberB, new { @data-ng-model = "numberB" })
<p>Result: {{ numberA + numberB }} </p>
</div>
Now I know the above will not work because I did some research and found out that this won't work but I could not find another way of completing my task. Most of the results showed me that I will need to create simple .html
files to make this work.
Question:
Is there a simple way of doing the above? For example something that lets me integrate
AngularJs
into Razor view easily?If the answer to above question is no, then what will be the best way to do the task above?
Is there no way to do this using
Razor Views
andAngularJs
together?