locked
How to get id from a td RRS feed

  • Question

  • User-1282355114 posted

    I am coding a game similar to chess and i need to get the id value of all td in my table in the c# controller to code all of the logic.

    I call a action method in my controller in this way:

    <td class="texto" id="0-0" onclick="location.href='@Url.Action("Action", "Controller")'">A</td>

    So i want to get de value 0-0 to use it, in my Action of the controller

    PD: i am new in asp, so i dont know if that's a correct way to do it.

    PD: sorry if i have bad english 

    Monday, September 7, 2020 7:07 AM

All replies

  • User475983607 posted

    Add the coordinates to the URL.  You mihr be interested in learning HTTP GET and POST; https://www.w3schools.com/tags/ref_httpmethods.asp

    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Sort a table</h1>
    
    <table>
        <thead>
            <tr>
                <td>Column 1</td>
                <td>Column 2</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="texto" id="0-0" onclick="location.href='@Url.Action("Cell", "Jquery", new {row=0, col=0 })'">(0,0)</td>
                <td class="texto" id="0-0" onclick="location.href='@Url.Action("Cell", "Jquery", new {rowx=0, col=1 })'">(0,1)</td>
            </tr>
            <tr>
                <td class="texto" id="0-0" onclick="location.href='@Url.Action("Cell", "Jquery", new {row=1, col=0 })'">(1,0)</td>
                <td class="texto" id="0-0" onclick="location.href='@Url.Action("Cell", "Jquery", new {row=1, col=1 })'">(1,1)</td>
            </tr>
        </tbody>
    </table>
    
    <hr />
    @if (ViewBag.Row != null)
    {
        <div>
            Row = @ViewBag.Row
        </div>
        <div>
            Col = @ViewBag.Col
        </div>
    }
        public class JqueryController : Controller
        {
            [HttpGet]
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpGet]
            public IActionResult Cell(int row, int col)
            {
                ViewBag.Col = col;
                ViewBag.Row = row;
                return View("Index");
            }
    
        }

    Monday, September 7, 2020 11:08 AM
  • User-1151440187 posted

    I am coding a game similar to chess and i need to get the id value of all td in my table in the c# controller to code all of the logic.

    I call a action method in my controller in this way:

    <td class="texto" id="0-0" onclick="location.href='@Url.Action("Action", "Controller")'">A</td>

    So i want to get de value 0-0 to use it, in my Action of the controller

    PD: i am new in asp, so i dont know if that's a correct way to do it.

    PD: sorry if i have bad english 

    Hii good morning.

    Just add this.

    <td class="texto" id="0-0" onclick="location.href='@Url.Action("Action", "Controller", new { ID = id} )'">A</td>

    And in controller,

    public ActionResult Display( int ID )

    {

    Your code here....

    }

    That's it.

    Hope it helps you.

    Thanks!

    Tuesday, September 8, 2020 3:50 AM
  • User1686398519 posted

    Hi shikitodoisa,

    According to your needs, you can use jquery to get the id of  the clicked td. I made a simple example, you can refer to it.

    • This example does not use the _Layout view.

    Controller

        public class GetIdFromTdController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public ActionResult SendTdId(string sendtdid)
            {
                sendtdid += " has been used.";
                return Json(sendtdid,JsonRequestBehavior.AllowGet);
            }
        }

    View

    @{
        ViewBag.Title = "Index"; 
        Layout = null;
    }
    <h2>Index</h2>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <table id="testtable" class="table table-bordered">
        @for (int i = 0; i < 10; i++)
        {
            <tr>
                @for (int j = 0; j < 9; j++)
                {
                    <td id="@i.ToString()-@j.ToString()">@i.ToString()-@j.ToString()</td>
                }
            </tr>
        }
    </table>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $("#testtable tr td").on("click", function (event) {
            var tdid = $(this).attr("id");
            $.ajax({
                url: "@Url.Action("SendTdId")",
                type: "POST",
                data: { sendtdid: tdid},
                success: function (data) {
                    alert(data);
                }
            });
        });
    </script>

    Here is the result.

    Best Regards,

    YihuiSun

    Tuesday, September 8, 2020 5:57 AM
  • User-1282355114 posted

    thank you so much for your answer, that's that i needed

    Tuesday, September 8, 2020 8:16 AM
  • User-1282355114 posted

    thank you so much for this answer i think i will use this way, actually i don't know how this forum works (how to give you points or something like that) but really, thank you so much

    Tuesday, September 8, 2020 8:23 AM
  • User1686398519 posted

    Hi shikitodoisa,

    In this forum, if you think an answer is helpful, you can click on the mark. This can also make it easier for more people who have the same problem to find the answer.

    Best Regards,

    YihuiSun

    Tuesday, September 8, 2020 9:03 AM