简体   繁体   中英

Can a single <input> element return more than one value?

VS2013, VB, MVC5, html

These posts ( multiple submits , which button is pressed ) show nicely how to use multiple < input > elements in a single form. This is nice for including a similar command at the end of each line in a list.

I want to have a page that lists multiple lines and has the same < input > elements at the end of each line. When a specific < input > is clicked, it must return to the 'controller method' information specific to that line and relevant to which link was clicked. So my display will look something like this:

This is line1   LINK1 | LINK2 | LINK3
This is line2   LINK1 | LINK2 | LINK3
This is line3   LINK1 | LINK2 | LINK3

The user will click on LINK1, LINK2, or LINK3 to accomplish some operation on line1, line2, or line3 based on which LINK is clicked. The actions might be something like Edit, Delete, List Users, etc.

The controller method will need data specific to the line on which the LINKs are clicked such as record ID, type of record, and so on just as an example. It could be the line was formed from multiple tables with different ID's; I'm just trying to establish that more than one piece of information is required in the transfer of control.

Can I provide multiple pieces of different data for each LINK, and how? I've only seen and learned how to get information back to the controller from the element by checking if the "ID" attribute is empty or not, and then capturing the data in the "Value" attribute per this post also linked above.

Is there a way to setup an element so that I can read other data from that specific element? The information has to be particular to that button and that line, meaning not something that is an overall form attribute.

Perhaps it is not possible by design, but I won't know if I don't ask.

Why don't you add as many forms as lines do you have? Something like:

<table>
    @foreach (obj item in Model)        
        {
        @using (Html.BeginForm())
            {
            <tr>
                 <td>
                 // your inputs and buttons here
                 </td>
            </tr>
            }
        } 
<table>

This way each form will only POST data relevant to your line.

you could go further than below with some ajax but this will do the trick and allow you to use multiple values from each "link" in each "row"

@using (Html.BeginForm())
{
    @Html.Hidden("myType", "")
    @Html.Hidden("myID","")
} 

This is line1   <a class="action-link" href="#" data-type="typeA" data-id="1">Link 1</a> | <a class="action-link" href="#" data-type="typeA" data-id="2">Link 2</a> | <a class="action-link" href="#" data-type="typeA" data-id="3">Link 3</a> 
This is line2   <a class="action-link" href="#" data-type="typeB" data-id="1">Link 1</a> | <a class="action-link" href="#" data-type="typeB" data-id="2">Link 2</a> | <a class="action-link" href="#" data-type="typeB" data-id="3">Link 3</a> 
etc....

<script>
$(document).ready(function() {
    $(".action-link").on("click", function(e) {
        e.preventDefault();

        var myType = $(this).attr("data-type");
        var myID = $(this).attr("data-id");

        //you could do some validation here...

        $("#myType").val(myType);
        $("#myID").val(myID);

        $("form:first").submit();
    });
});
</script>

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