Wednesday, June 2, 2021

MVC How to keep data on the same View page using ZLS action Attribute of Input tag

Related Topics
Index.cshtml View page code

        <form action="" method="post">
            <input type="text" name="number1" value="" />
            <br />
            <input type="text" name="number2" value="" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>


HomeController.cs Code:

using System;
using System.Web.Mvc;
namespace WebActions.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        // POST: Home
        [HttpPost]
        public ActionResult Index(int number1, int number2)
        {
            int sum = number1 + number2;
            Response.Write(sum);
            return View();
        }
        //Alternative using Request object
        [ActionName("Index")]
        public ActionResult IndexPost()
        {
            int num1 = Convert.ToInt32(Request["number1"]);
            int num2 = Convert.ToInt32(Request["number2"]);
            Response.Write(num1 + num2);
            return View();
        }
    }
}


OUTPUT


No comments:

Post a Comment

Hot Topics