Home How to add a value to a class if the class contains LIST<class>
Reply: 1

How to add a value to a class if the class contains LIST<class>

Kirill Zorin
1#
Kirill Zorin Published in 2018-02-11 09:51:34Z

I have a class graf.

public class Graf
    {
        public List<Point> first { get; set; }
        public List<Point> second { get; set; }
}

This class contains List

public class Point
    {
        public int x { get; set; }
        public int y { get; set; }


        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    } 

I need to add a Point into class Graf from index.cshtml:

@model WebApplication2.Models.Graf
<table>
        <tr>
            <td></td>
            <td>
                <div class="item">
                    <label>Y</label>
                    <input name="Y11" value="@Model.first" />  --------??
                </div>
            </td>
        </tr>
</table>
    <input type="submit" value="Send" />

But i dont now how i can input into Graf class Point?

How can I do it?

Artur Lavrov
2#
Artur Lavrov Reply to 2018-02-11 15:51:34Z

Ok. So let's start from a client-side code.I suppose that you have a next Index.cshtml view.

    <!-- You use this code to display data from your model -->
<table>
        <tr>
            <td></td>
            <td>
                <div class="item">
                    <label>Y</label>
                    <input name="Y11" value="@Model.first" />  --------??
                </div>
            </td>
        </tr>
</table>

Than you need a code that post new Point object from your view to controller.It could be like something like this:

<form asp-controller="Home" asp-action="InsertPoint" method="post">
    X value: <input type="text" name="x"><br>
    Y value: <input type="text" name="y"><br>
    <input type="submit" value="Submit">
</form>

In your controller you should create action with following signature

[HttpPost]
public async Task<IActionResult> InsertPoint(Point point)
{
    //Validation and insertion to list
    return View();
}

NB

It's not an ideal solution. You could perform this task in many different ways. My aim, is just to show you the basic idea how it could be done. If you need more information you could start from this article

And of course, keep in mind that google is your good friend.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317316 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO