FormCollection Empty on Form Post in ASP.NET-MVC

前端 未结 4 556
心在旅途
心在旅途 2021-01-03 18:33

I am posting a very simple form using a method I have used frequently in the past. It may be easier to show my code rather than type a lengthy explanation. Here\'s the HTML:

相关标签:
4条回答
  • 2021-01-03 18:59

    None of your inputs have a name attribute. No name = not in the FormCollection.

    0 讨论(0)
  • 2021-01-03 19:04

    I see that this question is already answered. Following is another approach I used in a MVC view, when the form collection was empty. Using JavaScript / jQuery, it sets name-value pairs to a hidden control which gets appended to the form.

    JavaScript / jQuery

     $("#btnExport").click(function (event) {
                    event.preventDefault();
    
                    //Append the parameter to the form
                    var practiceVal = $('#Practice').val();
                    $('<input />').attr('type', 'hidden')
                       .attr('name', "Practice").attr('value', practiceVal)
                       .appendTo('#formHome');
    
                    //Submit
                    $('#formHome').submit();
                });
    

    Form

     <form id="formHome" method="post">
     </form>
    

    Refer Forms

    When a form is submitted for processing, some controls have their name paired with their current value and these pairs are submitted with the form

    0 讨论(0)
  • 2021-01-03 19:07

    Wish I could post this as a simple comment, but I don't have that priviledge... I added all my name attributes, and still no joy. Remember to add your name attribute to your form itself. Must use the overload for HTML.BeginForm that accepts htmlAttributes.

    0 讨论(0)
  • 2021-01-03 19:08

    I had this issue and then realised I had disabled all the INPUT controls before I submitted the form (as a UX feature).

    0 讨论(0)
提交回复
热议问题