Click here to Skip to main content
15,898,134 members

C# MVC checkbox javascript ajax or C# action which way should I do?

TheBigBearNow asked:

Open original thread
Hello everyone,
I have a view that has a table with rows of records. In front of each row I added a separate checkbox. I am currently in the process of determinine if this checkbox ischecked or not. I was wondering if I should do my button click with javascript and ajax to determine what records have a selected checkbox with these checkboxes I will print out a rdlc report of what was selected. Or should I use a HTMLActionlink button to go straight to a controller and see what was selected. What way would be faster and better? Also what is the proper way I should do this?
C#
MVC View
<input type="button" class="btn btn-primary" name="command" id="btnGetChecks" value="Generate Selected" />
<table class="table" id="maintbl"><thead>…</thead>
<tbody id="maintblbody">
                @for (int i = 0; i < Model.ListQuotes.Count; i++)
                {
                    var current = Model.ListQuotes[i];
                    <tr>
                        <td>
                            @Html.CheckBox(i+"_Row", true, new { value = @Model.ListQuotes[i].QuoteID })
                            </td><td>
                            //<input type="checkbox" class="checkbox" name="checks" id="@(i + "_Row")" value="@Model.ListQuotes[i].QuoteID" />
                        </td>
                        @for (int j = 0; j < Model.Settings.Columns.Length; j++)
                        {
                            if (Model.Settings.Show[j])
                            {
                                var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();
                                <td style="padding-right:20px;">@quote.GetValue(current, null)</td>
                            }
                        }
                        <td>
                            <button type="button" class="openModal btn btn-danger" data-quoteid="@Model.ListQuotes[i].QuoteID">
                                Edit
                            </button>
                        </td>
                    </tr>
                }
</tbody>
<script>
$('#btnGetChecks').on('click', function () {
            var quotes = [];
            var quoteid = $(this).attr("value");
            var chkboxtable = $('#maintbl');
            var 
            //$('input:checked').each(function() {
            //    quotes.push($(this).attr("value"));
            //});    with 0 -  ^(0|\+?[1-9]\d*)$         /^-?[0-9]+$/
            // $(this).val($(this).val().replace(/[^\d].+/, ""));
            // if (!(keyCode >= 48 && keyCode <= 57)
            $('input[type=checkbox]').each(function () {
                if (this.checked==true) {
                    if (IsPositiveInteger(quoteid)) {
                        quotes.push($(this).attr("value"));

                    }
                }
            });

            alert(quotes);
        });
<script>


What I have tried:

Here is what I currently have im very curious to hear what you all have to say. I have the id of each quote attached to the checkbox. I wanted to push the valid ones to an array but so far my current code doesn’t do it yet. I feel I am close to having it.
Tags: C#, Javascript, Ajax, MVC5

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900