locked
Process Form Data RRS feed

  • Question

  • User-624973828 posted

    Hello

    From a simple form in the Index.cshtml file I want to send a Number to the Index.cshtml.cs file so that for example 25% is calculated and return the result and print it on the same page (Index.cshtml) of the form.

    Thank you

    Thursday, October 29, 2020 1:32 PM

Answers

  • User475983607 posted

    thanks though does not recognize ViewBag, only ViewData
    Replacing ViewBag.Number with ViewData ["Number"] does not show an error, but passing a number does not print anything.
    Is there another way?

    The code shared above has been tested and it works just fine.  Share you code if you need a community code review.  Perhaps you are building a Razor Pages application?

    @page
    @model RazorDemo.Pages.FormModel
    @{
    }
    
    <div>
        <form id="form" action="" method="post">
            <div>
                <input name="number" type="text" />
            </div>
            <div>
                <input id="Submit1" type="submit" value="submit" />
            </div>
        </form>
        <div>
            <span>25% is @Model.Number</span>     
        </div>
    </div>
        public class FormModel : PageModel
        {
            public void OnGet()
            {
            }
    
            public void OnPost(decimal number)
            {
                Number = (number * 0.25m);
            }
    
            [BindProperty]
            public decimal Number { get; set; }
        }

    Razor Pages Tutorials

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-3.1&tabs=visual-studio

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2020 2:27 PM

All replies

  • User475983607 posted

    From a simple form in the Index.cshtml file I want to send a Number to the Index.cshtml.cs file so that for example 25% is calculated and return the result and print it on the same page (Index.cshtml) of the form.

    Any beginning level tutorial illustrates how to post form data.  

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-3.1&tabs=visual-studio

    public IActionResult Index()
    {
        return View();
    }
    
    [HttpPost]
    public IActionResult Index(decimal number)
    {
        ViewBag.Number = (number * 0.25m).ToString();
        return View();
    }
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <div class="table-responsive">
        <form id="form" action="" method="post">
            <div>
                <input name="number" type="text" />
            </div>
            <div>
                <input id="Submit1" type="submit" value="submit" />
            </div>
        </form>
        <div>
            @if(ViewBag.Number != null)
            {
            <span>25% is @ViewBag.Number</span> 
            }
        </div>
    </div>
    
    @section scripts {
    <script>
    
    </script>
    }
    

    Thursday, October 29, 2020 1:49 PM
  • User-624973828 posted

    thanks though does not recognize ViewBag, only ViewData
    Replacing ViewBag.Number with ViewData ["Number"] does not show an error, but passing a number does not print anything.
    Is there another way?

    Thursday, October 29, 2020 2:13 PM
  • User475983607 posted

    thanks though does not recognize ViewBag, only ViewData
    Replacing ViewBag.Number with ViewData ["Number"] does not show an error, but passing a number does not print anything.
    Is there another way?

    The code shared above has been tested and it works just fine.  Share you code if you need a community code review.  Perhaps you are building a Razor Pages application?

    @page
    @model RazorDemo.Pages.FormModel
    @{
    }
    
    <div>
        <form id="form" action="" method="post">
            <div>
                <input name="number" type="text" />
            </div>
            <div>
                <input id="Submit1" type="submit" value="submit" />
            </div>
        </form>
        <div>
            <span>25% is @Model.Number</span>     
        </div>
    </div>
        public class FormModel : PageModel
        {
            public void OnGet()
            {
            }
    
            public void OnPost(decimal number)
            {
                Number = (number * 0.25m);
            }
    
            [BindProperty]
            public decimal Number { get; set; }
        }

    Razor Pages Tutorials

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-3.1&tabs=visual-studio

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2020 2:27 PM
  • User-624973828 posted

    Excellent

    If they were Razor Pages

    Just one more thing, how to keep the number entered in the Form?

    Thursday, October 29, 2020 2:57 PM
  • User475983607 posted

    Just one more thing, how to keep the number entered in the Form?

    Typically a database but ultimately it depends on the design.  Any beginning level tutorial covers the basics.   

    Thursday, October 29, 2020 3:19 PM