0

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.

Community
  • 1
  • 1
Alan
  • 1,587
  • 3
  • 23
  • 43
  • I don't really understand your question, so some code would help. – Harkály Gergő Mar 10 '15 at 15:51
  • 1
    You seem to be confused about a lot of things.. for instance, I don't see where you've even mentioned an `` element anywhere in your question, it's almost entirely about links... which are not input elements. – Erik Funkenbusch Mar 10 '15 at 18:46
  • @Gergo. I had thought showing what I was looking for was more useful, but once again I am legitimately reminded some find actual code more useful, so, good point. I will make an addition to the original post to make it more clear. – Alan Mar 10 '15 at 22:40
  • @ErikFunkenbusch. Good point also. I styled the < input > element as text and fell victim to my own technique as I wrote the question. It's not a link, but an < input > element that looks like a link. When I update the post per Gergo above I'll add that to clarify. – Alan Mar 10 '15 at 22:44

2 Answers2

1

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.

SmartDev
  • 2,802
  • 1
  • 17
  • 22
  • Technically, this is not valid HTML, though it will usually work with most browsers... – Erik Funkenbusch Mar 10 '15 at 18:41
  • @ErikFunkenbusch That is correct, but not what I wanted to point out. Just remote the table or move the form inside td's to make the code HTML compliant. – SmartDev Mar 10 '15 at 19:33
  • @SmartDev. Perfect. I'm not as fully conversant yet as I need to be with HTML and didn't know I could do what you suggest, but that is exactly the kind of solution I was looking for and I understand how to code it. I also get that this is in Razor that will be converted to HTML. – Alan Mar 10 '15 at 22:50
  • @ErikFunkenbusch. I thought once the server converts the Razor to HTML it's a legitimate HTML page. What part won't work with some browsers? Do some browsers not accept multiple form sections per page? – Alan Mar 10 '15 at 22:53
  • @Alan - Razor will do whatever you tell it to, it doesn't mean it's valid HTML. In this case, it's not valid to place form elements within a tr element. The only valid elements in a tr are td or th. – Erik Funkenbusch Mar 11 '15 at 03:53
  • @ErikFunkenbusch. Although th/td are the only valid elements in a tr, does your statement imply there's a restriction on what happens inside a th/td? I spent some time researching after reading your comment, and it seems that many different elements can be used inside the td, so I want to make sure I understand your point correctly. – Alan Mar 11 '15 at 17:19
  • @Alan - The above code places the Form element inside the tr. That is the code I said was invalid. I said nothing about being inside a td or th, as you are correct. However, you cannot span a form tag across multiple td's or th's in a single row because of this. – Erik Funkenbusch Mar 11 '15 at 17:55
1

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>
scgough
  • 5,099
  • 3
  • 30
  • 48
  • While I didn't state it explicitly other than listing html at the head, I am actually trying to do it in HTML, if possible. HOWEVER, this is certainly a great option and your explicit example is very useful for me from a script point of view. +1. – Alan Mar 10 '15 at 22:38
  • Glad to have been of assistance Alan – scgough Mar 10 '15 at 23:49