Thursday, 20 May 2021

how to sum two number using model and javascript without controller in asp.net mvc

  

Overview:


This article explains how to create a  sum of two numbers using model and javascript without a controller action in ASP.NET MVC. then pass the data from the view to the javascript function using by model. The following steps here.


Step-1:

Create a new project in visual studio 2019.



Step-2:

Create a Model Class in Model folder - Right click on Model folder - Add new class - enter the class name as SumModels.cs


Step-3:

if you don't have a controller, Create a new controller in the controller folder. - Right-click on Model folder- Add new controller class enter the name as HomeController.cs

Or  HomeController.cs is created automatically when you create a project.


Step-4:

Create a new view page from the controller- Right-click on the Controller action method then create the view.



Model Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace MVCJavascriptSum.Models

{

    public class SumModel

    {

        public int a { get; set; }

        public int b { get; set; }


        public int total { get; set; }

    }

}


Controller Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MVCJavascriptSum.Models;


namespace MVCJavascriptSum.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }


        public ActionResult About()

        {

            ViewBag.Message = "Your application description page.";


            return View();

        }


        public ActionResult Contact()

        {

            ViewBag.Message = "Your contact page.";


            return View();

        }


        [HttpGet]

        public ActionResult Sum()

        {

            return View();

        }

        [HttpPost]


        public ActionResult Sum(SumModel sumModel)

        {


            return View();

        }

    }

}


View Code:


@model MVCJavascriptSum.Models.SumModel


@{

    ViewBag.Title = "Sum";

}


<h2>Sum</h2>

@using (Html.BeginForm("Sum", "Home"))

{

    <label>A value:</label>

    @Html.TextBoxFor(Model => Model.a, new { @class = "form-control", @id = "txta" })

    <label>B value:</label>

    @Html.TextBoxFor(Model => Model.b, new { @class = "form-control", @onchange = "add()", @id = "txtb" })

    <br />


    <div class="col-lg-push-2">

        <div class="col-lg-2"><label>Total:</label></div>

        @Html.HiddenFor(Model => Model.total, new { @class = "form-control", @id = "total" })

        @Html.TextBoxFor(Model => Model.total, new { @class = "form-control", style = "width:100px", @disabled = "true" })


    </div>


    <button id="sum" type="submit">Sum</button>

}


<script type="text/javascript">

    function add() {

        var a, b, total;

        a = Number(document.getElementById("txta").value);

        b = Number(document.getElementById("txtb").value);

        total = a + b;

        //alert(total);

        document.getElementById("total").value = total;


        //  alert(total);

        $("#total").value = total

    }


</script>


Route Config.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;


namespace MVCJavascriptSum

{

    public class RouteConfig

    {

        public static void RegisterRoutes(RouteCollection routes)

        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(

                name: "Default",

                url: "{controller}/{action}/{id}",

                defaults: new { controller = "Home", action = "Sum", id = UrlParameter.Optional }

            );

        }

    }

}

Final Output:

sumoutput

Demo:

demo sum two numbers



More Details:


https://youtu.be/y2x77PLjaMc

Summary

 This tutorial explains, how to create a sum of two numbers using model and javascript in asp.net MVC C#. Learn more and share with your friends and relatives. any comments are given below.

Thank you for reading my blogs..!!


Saturday, 8 May 2021

how to sum two number in mvc

 Overview:


This article explains how to create a  sum of two numbers using model, controller, view in ASP.NET MVC. then pass the data from the view to the controller using by model. The following steps here.

DownloadCode.zip

Step-1:

Create a new project in visual studio 2019.


sum two numbers mvc






Step-2:

Create a Model Class in Model folder - Right click on Model folder - Add new class - enter the class name as SumModels.cs



create model mvc

Step-3:

if you don't have a controller, Create a new controller in the controller folder. - Right-click on Model folder- Add new controller class enter the name as HomeController.cs

Or  HomeController.cs is created automatically when you create a project.

sum two numbers mvc




Step-4:

Create a new view page from the controller- Right-click on the Controller action method then create the view.

add two numbers mvc




Model Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace AddTwoNumberMVC.Models

{

    public class SumModels

    {

        public int A { get; set; }

        public int B { get; set; }


        public int Total { get; set; }

    }

}


Controller code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using AddTwoNumberMVC.Models;



namespace AddTwoNumberMVC.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }


        public ActionResult About()

        {

            ViewBag.Message = "Your application description page.";


            return View();

        }


        public ActionResult Contact()

        {

            ViewBag.Message = "Your contact page.";


            return View();

        }


        [HttpGet]

        public ActionResult Sum()

        {

            return View();

        }


        [HttpPost]

        public ActionResult Sum(SumModels sum)

        {

            sum.Total = sum.A + sum.B;


            return View(sum);

        }

    }

}


View Code:

@model AddTwoNumberMVC.Models.SumModels


@{

    ViewBag.Title = "Sum";

}


<h2>Sum</h2>


<div class="row">

    @using (Html.BeginForm("Sum", "Home"))

    {

        <div class="col-lg-3">A value :@Html.TextBoxFor(Model => Model.A, new { @class = "form-control" })</div>

        <div class="col-lg-3">B value:@Html.TextBoxFor(Model => Model.B, new { @class = "form-control" })</div>

        <br />

        <div class="col-lg-3">Total:<wbr /><wbr /> @Html.DisplayFor(Model => Model.Total, new { @class = "form-control" })</div>

        <div class="col-lg-3"><button type="submit" value="Sum" class="btn btn-primary">Sum</button></div>

    }

</div>


Final Output:

sum two numbers in mvc




More Details:

https://www.youtube.com/watch?v=6R__xst_PMk


Summary

 This tutorial explains, how to create a sum of two numbers in asp.net MVC C#. Learn more and share with your friends and relatives. any comments are given below.

Thank you for reading my blogs..!!

ASP.NET MVC Full Course | In Tamil

  Overview: In this article, we will learn ASP.NET MVC Full Course in Tamil. We can learn additionally C#, SQL,  Bootstrap ..etc.  Model Pop...