locked
Calculation RRS feed

  • Question

  • User733430484 posted

    Hi All,

    Actually I try to learn how to do calculation in webpages.

    Let said we have 2 model . first named XY and second XFYF .Every model have 4 row.

    I create new folder in root solution name ViewModels for result of calculation between XY and XFYF.

    ViewModels have model name Detailiist.

    All value in XY multifly with XFYF then result we have put into DetailLiist. 

    what show in web pages is;

    X           Y           XF          YF              R_X             R_Y
    2.01      10.24      0           0                0                  0
    4.02      21.12      1           1              4.02              21.12
    6.06      31.07      2           2              12.12            62.14
    8.02      42.04      3           3               24.06           126.12

    1.Controller

    using Calculate_1.Models;
    using Calculate_1.ViewModels;
    using System.Collections.Generic;
    using System.Web.Mvc;
    
    namespace Calculate_1.Controllers
    {
        public class HomeController : Controller
        {
            // GET: Home
            public ActionResult Index()
            {
                List<XY> FillDatalist = new List<XY> {
                    new XY{X=2.01m,Y=10.24m},
                    new XY{X=4.02m,Y=21.12m},
                    new XY{X=6.06m,Y=31.07m},
                    new XY{X=8.02m,Y=42.04m}
                    };
    
                List<XFYF> FillDatalist_Factor = new List<XFYF> {
                    new XFYF{XF=0,YF=0},
                    new XFYF{XF=1,YF=1},
                    new XFYF{XF=2,YF=2},
                    new XFYF{XF=3,YF=3}
                    };
    
                var Result = new List<DetailLiist>();
    
                foreach (var first in FillDatalist)
                {
                    foreach (var second in FillDatalist_Factor)
                    {
                        new DetailLiist { R_X = second.XF * first.X, R_Y = second.YF * first.Y };
                    }
    
                }
                return View(Result);
            }
        }
    }
    

    2.Model 

    namespace Calculate_1.Models
    {
        public class XY
        {
            public decimal X { get; set; }
            public decimal Y { get; set; }
        }
        public class XFYF
        {
            public decimal XF { get; set; }
            public decimal YF { get; set; }
        }
    }
    
    

    3.Model ViewModels

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Calculate_1.Models;
    
    namespace Calculate_1.ViewModels
    {
        public class DetailLiist
        {
            public decimal R_X { get; set; }
            public decimal R_Y { get; set; }
        }
    }
    

    4.View

    @model IEnumerable<Calculate_1.ViewModels.DetailLiist>
    
    
    <html>
    <body>
        @if (Model.Count() > 0)
        {
            <hr />
            <table class="table">
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.R_X</td>
                        <td>@item.R_Y</td>
                    </tr>
                }
            </table>
        }
    
    
    </body>
    </html>
    

    Thank.

    Tuesday, June 9, 2020 4:15 AM

Answers

  • User-719153870 posted

    Hi sy_60,

    X           Y           XF          YF              R_X             R_Y
    2.01      10.24      0           0                0                  0
    4.02      21.12      1           1              4.02              21.12
    6.06      31.07      2           2              12.12            62.14
    8.02      42.04      3           3               24.06           126.12

    If this is the expected output, then i'd suggest to update your DetailLiist to include other 4 fileds.

                foreach (var first in FillDatalist)
                {
                    foreach (var second in FillDatalist_Factor)
                    {
                        new DetailLiist { R_X = second.XF * first.X, R_Y = second.YF * first.Y };
                    }
    
                }

    These two foreach loop will cause 16 records be returned, and the list Result never got updated in your code.

    Please refer to below demo built based on your code:

    cshtml:

    @model IEnumerable<MVCDemo01.ViewModels.DetailLiist>
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Calculation</title>
    </head>
    <body>
        <div>
            @if (Model.Count() > 0)
            {
                <hr />
                <table class="table">
                    <tr>
                        <td>@Html.DisplayNameFor(model => model.X)</td>
                        <td>@Html.DisplayNameFor(model => model.Y)</td>
                        <td>@Html.DisplayNameFor(model => model.XF)</td>
                        <td>@Html.DisplayNameFor(model => model.YF)</td>
                        <td>@Html.DisplayNameFor(model => model.R_X)</td>
                        <td>@Html.DisplayNameFor(model => model.R_Y)</td>
                    </tr>
                    @foreach (var item in Model)
                    {
                <tr>
                    <td>@item.X</td>
                    <td>@item.Y</td>
                    <td>@item.XF</td>
                    <td>@item.YF</td>
                    <td>@item.R_X</td>
                    <td>@item.R_Y</td>
                </tr>
                    }
                </table>
            }
        </div>
    </body>
    </html>

    controller:

            public ActionResult Calculation()
            {
                List<XY> FillDatalist = new List<XY> {
                    new XY{X=2.01m,Y=10.24m},
                    new XY{X=4.02m,Y=21.12m},
                    new XY{X=6.06m,Y=31.07m},
                    new XY{X=8.02m,Y=42.04m}
                    };
    
                List<XFYF> FillDatalist_Factor = new List<XFYF> {
                    new XFYF{XF=0,YF=0},
                    new XFYF{XF=1,YF=1},
                    new XFYF{XF=2,YF=2},
                    new XFYF{XF=3,YF=3}
                    };
    
                var Result = new List<DetailLiist>();
    
                for (int i = 0; i < FillDatalist.Count; i++)
                {
                    Result.Add(new DetailLiist { X=FillDatalist[i].X,Y=FillDatalist[i].Y,XF=FillDatalist_Factor[i].XF,YF=FillDatalist_Factor[i].YF, R_X = FillDatalist_Factor[i].XF * FillDatalist[i].X, R_Y = FillDatalist_Factor[i].YF * FillDatalist[i].Y });
                }
                return View(Result);
            }

    models:

        public class XY
        {
            public decimal X { get; set; }
            public decimal Y { get; set; }
        }
        public class XFYF
        {
            public decimal XF { get; set; }
            public decimal YF { get; set; }
        }
        public class DetailLiist
        {
            public decimal X { get; set; }
            public decimal Y { get; set; }
            public decimal XF { get; set; }
            public decimal YF { get; set; }
            public decimal R_X { get; set; }
            public decimal R_Y { get; set; }
        }

    Here's the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2020 6:16 AM

All replies

  • User-719153870 posted

    Hi sy_60,

    X           Y           XF          YF              R_X             R_Y
    2.01      10.24      0           0                0                  0
    4.02      21.12      1           1              4.02              21.12
    6.06      31.07      2           2              12.12            62.14
    8.02      42.04      3           3               24.06           126.12

    If this is the expected output, then i'd suggest to update your DetailLiist to include other 4 fileds.

                foreach (var first in FillDatalist)
                {
                    foreach (var second in FillDatalist_Factor)
                    {
                        new DetailLiist { R_X = second.XF * first.X, R_Y = second.YF * first.Y };
                    }
    
                }

    These two foreach loop will cause 16 records be returned, and the list Result never got updated in your code.

    Please refer to below demo built based on your code:

    cshtml:

    @model IEnumerable<MVCDemo01.ViewModels.DetailLiist>
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Calculation</title>
    </head>
    <body>
        <div>
            @if (Model.Count() > 0)
            {
                <hr />
                <table class="table">
                    <tr>
                        <td>@Html.DisplayNameFor(model => model.X)</td>
                        <td>@Html.DisplayNameFor(model => model.Y)</td>
                        <td>@Html.DisplayNameFor(model => model.XF)</td>
                        <td>@Html.DisplayNameFor(model => model.YF)</td>
                        <td>@Html.DisplayNameFor(model => model.R_X)</td>
                        <td>@Html.DisplayNameFor(model => model.R_Y)</td>
                    </tr>
                    @foreach (var item in Model)
                    {
                <tr>
                    <td>@item.X</td>
                    <td>@item.Y</td>
                    <td>@item.XF</td>
                    <td>@item.YF</td>
                    <td>@item.R_X</td>
                    <td>@item.R_Y</td>
                </tr>
                    }
                </table>
            }
        </div>
    </body>
    </html>

    controller:

            public ActionResult Calculation()
            {
                List<XY> FillDatalist = new List<XY> {
                    new XY{X=2.01m,Y=10.24m},
                    new XY{X=4.02m,Y=21.12m},
                    new XY{X=6.06m,Y=31.07m},
                    new XY{X=8.02m,Y=42.04m}
                    };
    
                List<XFYF> FillDatalist_Factor = new List<XFYF> {
                    new XFYF{XF=0,YF=0},
                    new XFYF{XF=1,YF=1},
                    new XFYF{XF=2,YF=2},
                    new XFYF{XF=3,YF=3}
                    };
    
                var Result = new List<DetailLiist>();
    
                for (int i = 0; i < FillDatalist.Count; i++)
                {
                    Result.Add(new DetailLiist { X=FillDatalist[i].X,Y=FillDatalist[i].Y,XF=FillDatalist_Factor[i].XF,YF=FillDatalist_Factor[i].YF, R_X = FillDatalist_Factor[i].XF * FillDatalist[i].X, R_Y = FillDatalist_Factor[i].YF * FillDatalist[i].Y });
                }
                return View(Result);
            }

    models:

        public class XY
        {
            public decimal X { get; set; }
            public decimal Y { get; set; }
        }
        public class XFYF
        {
            public decimal XF { get; set; }
            public decimal YF { get; set; }
        }
        public class DetailLiist
        {
            public decimal X { get; set; }
            public decimal Y { get; set; }
            public decimal XF { get; set; }
            public decimal YF { get; set; }
            public decimal R_X { get; set; }
            public decimal R_Y { get; set; }
        }

    Here's the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2020 6:16 AM
  • User733430484 posted

    Thank you very much Yang Shen.

    It working .

    Wednesday, June 10, 2020 1:17 AM