locked
Why is this code not work (ASP.NET MVC Core) RRS feed

  • Question

  • User-826336654 posted

    Hi All,

    I am learning ASP.NET MVC Core 3.1. I have created a New ASP.NET MVC Core 3.1 Project. What I want to achieve is to Add Two Decimal Numbers and show the result in the View. But I can't get the result in my view. Screen shot is below.

    Any help or suggestion will be greatly appreciated. My code is below.

    Model

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace WebApplication15.Models
    {
        public class MathsModel
        {
            [Display(Name = "First Number:")]
            [Required(ErrorMessage = "First Number is required.")]
            public decimal FirstNumber { get; set; }
    
            [Display(Name = "Second Number:")]
            [Required(ErrorMessage = "Second Number is required.")]
            public decimal SecondNumber { get; set; }
    
            [Display(Name = "Result:")]
            public decimal Result { get; set; }
        }
    }

    Controller:

    using Microsoft.AspNetCore.Mvc;
    using WebApplication15.Models;
    
    namespace WebApplication15.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult Index(MathsModel maths)
            {
                if (ModelState.IsValid)
                {
                    decimal firstnum = maths.FirstNumber;
                    decimal secondnum = maths.SecondNumber;
    
                    decimal result = firstnum + secondnum;
                }
                return View(maths);
            }
        }
    }
    

    View:

    @model WebApplication15.Models.MathsModel
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Index">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="FirstNumber" class="control-label"></label>
                    <input asp-for="FirstNumber" class="form-control" />
                    <span asp-validation-for="FirstNumber" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="SecondNumber" class="control-label"></label>
                    <input asp-for="SecondNumber" class="form-control" />
                    <span asp-validation-for="SecondNumber" class="text-danger"></span>
                </div>
                <div class="form-group">
                    @if (Model != null)
                    {
                        <label class="control-label">@Model.Result</label>
                    }
                    else
                    {
                        <label class="control-label">0</label>
                    }
                </div>
                <div class="form-group">
                    <input type="submit" value="Show Result" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    

    Thanks in Advance.

    Sunday, October 11, 2020 7:27 AM

Answers

  • User475983607 posted

    You forgot to set the Result property.

    [HttpPost]
    public ActionResult Index(MathsModel maths)
    {
        if (ModelState.IsValid)
        {
            decimal firstnum = maths.FirstNumber;
            decimal secondnum = maths.SecondNumber;
    
            decimal result = firstnum + secondnum;
    
            maths.Result = result;
        }
        return View(maths);
    }

    Be aware, you should not modify the data bound model within the body of an Action.  A better approach is creating a new model.  Typically, the model is saved and the result is a redirect.

    [HttpPost]
    public ActionResult Index(MathsModel maths)
    {
        if (ModelState.IsValid)
        {
            decimal firstnum = maths.FirstNumber;
            decimal secondnum = maths.SecondNumber;
    
            decimal result = firstnum + secondnum;
    
            MathsModel model = new MathsModel()
            {
                FirstNumber = firstnum,
                SecondNumber = secondnum,
                Result = result
            };
            return View(model);
        }
        //show error
        return View(maths);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 11, 2020 12:03 PM