locked
How to Same value in two List in asp mvc RRS feed

  • Question

  • User-519408813 posted

    Hay guest, I have case Like This, I have two List Data Like :

    Data 1 : A,B,A,C,A

    Data 2 : A,C,A,B,A

    I want If data 1 == data 2 show True/False or Color Green/Red -> I want Result Like This Data 3 = A:A green,B:C red,A:A green,C:B red,A:A green

    How To make this Case in ASP MVC ?

    Friday, June 5, 2020 1:42 PM

All replies

  • User1686398519 posted

    Hi,  Sepjak

    According to your needs, I made an example. This example uses an ajax request to pass data to the controller and return the result.

    Controller

            public ActionResult TestListResult()
            {
                return View();
            }
            [HttpPost]
            public ActionResult TestListResultGet(Array List1, Array List2)
            {
                //List<string> test1 = new List<string>{ "A", "B", "A", "C", "A" };
                //List<string> test2 = new List<string> { "A", "C", "A", "B", "A" };
                List<Boolean> test3= new List<Boolean>();
                for(int i = 0; i < List1.Length; i++)
                {
                    if (List1.GetValue(i).Equals(List2.GetValue(i)))
                    {
                        test3.Add(true);
                    }
                    else{
                        test3.Add(false);
                    }
                }
                return Json(test3,JsonRequestBehavior.AllowGet);
            }

    TestListResult

    <button>getResult</button>
    <table class="table">
        <thead>
            <tr>
                <td>List1</td>
                <td>List2</td>
                <td>Result</td>
            </tr>
        </thead>
        <tbody>
            <tr><td>A</td><td>A</td><td></td></tr>
            <tr><td>B</td><td>C</td><td></td></tr>
            <tr><td>A</td><td>A</td><td></td></tr>
            <tr><td>C</td><td>B</td><td></td></tr>
            <tr><td>A</td><td>A</td><td></td></tr>
        </tbody>
    </table>
    @section scripts{
        <script>
            $("button").click(function () {
                var List1Array = new Array();
                var List2Array = new Array();
                $('table tbody tr').each(function () {
                    $(this).children('td:eq(0)').each(function () {
                        List1Array.push($(this).text());
                    });
                    $(this).children('td:eq(1)').each(function () {
                        List2Array.push($(this).text());
                    });
                });
                $.ajax({
                    url: '/Home/TestListResultGet',
                    dataType: 'json',
                    type: "POST",
                    data: { List1: List1Array, List2: List2Array },
                    success: function (data) {
                        $('table tbody tr').each(function () {
                            $(this).children('td:eq(2)').each(function (i) {
                                $(this).empty();
                            });
                        });
                        $('table tbody tr').each(function (i) {
                            $(this).children('td:eq(2)').each(function () {
                                if (data[i]) {
                                    $(this).prepend('<span class="alert-success">' + data[i] + '</span>');
                                } else {
                                    $(this).prepend('<span class="alert-danger">' + data[i] + '</span>');
                                }
                            });
                        });
                    }
                });
            });
        </script>
    }
    

    Here is the result.

     
    Best Regards,

    YihuiSun

    Monday, June 8, 2020 4:46 AM