简体   繁体   中英

How can I Get a C# Variable assigned based on click with WebMatrix razor?

I am having trouble approaching a solution to my current situation:

I need to assign a dynamic value to a C# variable since C# is what queries my database.

This value will have to match the Primary Key ID for their selected (clicked upon, etc.) sql queried entry.

I never know how many entries (or which ones) there will be based on their search.

So far, they put their search conditions into a form, the database is dynamically queried, and the results are displayed in order row after row.

Now, I need a button (or some kind of user selected interface) that can tell me which entry they choose to edit, and then take the values of that entry in the database to the View & Edit page.

I can do all of this except, how can I tell which entry they click on (or otherwise choose) when C# is already done rendering the page by the time the user can give any input? JavaScript can't query a database, so how is this possible? Keep in mind, I know jQuery, but don't know any AJAX.

I am using WebMatrix C# razor, html, css, JavaScript, SQL Server Compact, and jQuery (standard WebMatrix setup, I'm sure).

Any help is much appreciated.

Ok, as GmG had commented, the webGrid is designed to help with this issue, however, if you prefer not to use that answer and construct your own, here is what I found that worked perfectly for me (even better for my specific case):

Within the @foreach(var row in db.Query("queryString")) loop, I (among the summarized information) added a form so that each entry found, would have its own form. In the form I placed only a submit button and a hidden input text element with any given specified name set in the "name" attribute (ie, name="inputName"). The value of this hidden element was @row.EntryID (ie, value="@row.EntryID"), which would plot the value of this field with the unique primary key of the entry found in the database. With the form action set to another file (ie, action="/somefile.cshtml") in that page, I simply extracted the EntryID from the hidden input text field (ie, var someVariable = Request.Form["inputName"];). Since the request would only grab from whichever form submitted it and the value of the hidden input field was dynamically plotted, I can always get the ID of the entry they clicked the button for.

I hope this helps anyone who wants to perform a similar function without using the WebGrid helper (although, you probably want to, I mean that is what its there for). As for some like me, however, we may know how to better control it this way (because we may not know jack about helpers and how to tweak the little minute configurations therein required to display, style, and control the web interface just the way we want to).

Anyway, again, hope this helps someone.

It's quite late but it might help other:

foreach example

                @foreach(var row in sCustomersHO){
                <tr>
                    <td><a href="~/Admin/ManageCustomersCH?id=@row.CustId">@row.Account</a></td>
                    <td>@row.CustomerName</td>
                    <td>@row.RepName</td>
                    <td>@row.CustomerTypeDescription</td>
                    <td>@row.StopStatus</td>
                    <td><a href="~/Admin/DeleteCustomerHO?id=@row.CustId">Delete</a></td>
                </tr>
            }

grid example:

            @gAllReports.GetHtml(
            tableStyle: "grid",
            headerStyle: "head",
            alternatingRowStyle: "alt",
            columns: gAllReports.Columns(
                gAllReports.Column("Name"),
                gAllReports.Column("Description", format: @<textarea disabled>@item.Description</textarea>),
                gAllReports.Column("Action", format: @<form method="post">@AntiForgery.GetHtml()<input type="hidden" id="UserId" name="UserId" value="@UserId" /><input type="hidden" id="ReportId" name="ReportId" value="@item.ReportId" /><button type="submit" name="action" value="add">Add</button></form>)
            )
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM