locked
Hold the data into Model RRS feed

  • Question

  • User733430484 posted

    Hi All,

    How can I pass the Data to the Model and then show in View.

    Value X and Y is decimal number.Bellow that I try to do.

    somebody help me.

    1.Controller

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.IO;
    using System.Data;
    using System.Data.OleDb;
    using System.Configuration;
    using Calculate.Models;
    
    
    namespace Calculate.Controllers
    {
        public class HomeController : Controller
        {
            // GET: Home
            public ActionResult Index()
            {
                List<decimal> valX = new List<decimal>();
                decimal d1 = 2.01m;
                decimal d2 = 4.02m;
                decimal d3 = 6.06m;
                decimal d4 = 8.02m;
    
                List<decimal> valY = new List<decimal>();
                decimal dd1 = 10.24m;
                decimal dd2 = 21.12m;
                decimal dd3 = 31.07m;
                decimal dd4 = 42.04m;
    
                {
                    valX.Add(d1);
                    valX.Add(d2);
                    valX.Add(d3);
                    valX.Add(d4);
                    
    
                    valY.Add(dd1);
                    valY.Add(dd2);
                    valY.Add(dd3);
                    valY.Add(dd4);
    
                    List<XY> FillData = new List<XY>();
                    FillData.Add(valX);
                    FillData.Add(valY);
    
                }
                return View();
            }
        }
    }

    2.Model

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    namespace Calculate.Models
    {
        public class XY
        {
            public decimal X { get; set; }
            public decimal Y { get; set; }
        }
    }

    3.View

    @using Calculate.Models
    @Model 
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <!DOCTYPE html>
    
    <html>
    <body>
        }
        @if (Model.Count() > 0)
        {
            <hr />
            <table cellpadding="0" cellspacing="0">
                @foreach (XY data in Model)
                {
                    <tr>
                        <td>@data.X</td>
                        <td>@data.Y</td>
                    </tr>
                }
            </table>
    
        }
        </body>
    </html>

    Thank.

    Friday, May 29, 2020 4:20 AM

Answers

  • User1686398519 posted

    Hi,  sy_60

    • I modified your code according to your needs, please refer to it.
    • For more information on how to bind the model, you can refer to the two links: link1,link2.

    Controller

    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}
         };
         return View(FillDatalist);
    }

    Index

    @model IEnumerable<WebApplication1.Models.XY>
    
    @if (Model.Count() > 0)
    {
        <hr />
        <table class="table">
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.X</td>
                    <td>@item.Y</td>
                </tr>
            }
        </table>
    
    }

    Here is the result.

     
    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 6:11 AM

All replies

  • User1686398519 posted

    Hi,  sy_60

    • I modified your code according to your needs, please refer to it.
    • For more information on how to bind the model, you can refer to the two links: link1,link2.

    Controller

    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}
         };
         return View(FillDatalist);
    }

    Index

    @model IEnumerable<WebApplication1.Models.XY>
    
    @if (Model.Count() > 0)
    {
        <hr />
        <table class="table">
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.X</td>
                    <td>@item.Y</td>
                </tr>
            }
        </table>
    
    }

    Here is the result.

     
    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 6:11 AM
  • User733430484 posted

    Thank you very ,very much YuhuiSun,

    code working.

    Friday, May 29, 2020 1:56 PM