0

i need help with ASP MVC. I have:

<% using (Html.BeginForm(null,null,FormMethod.Post)) {%>
    <p>
         <input type="submit" value="Choose" />
          .
          .
          .
          <%=Html.ActionLink("Save", "Index")%>
    </p>
<% } %>

And i need to submit the form by that ActionLink, because i need to get selectedValue from dropdownlist and i do not know any way how to do that else then from FormCollection or parameter in method with attribute POST.

So i need to call this function after click on action link.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection formValues)
    {
    }

I tried something with jquery and Ajax.ActionLink but everything called the GET method of Index. (new { onclick = "$(this).parents('form').first().submit();" - did not work for me -it also called the GET method)

BradBrening
  • 5,418
  • 25
  • 30
Petr Pražák
  • 191
  • 2
  • 10

3 Answers3

1

You need to call the form's submit and then return false. Javascript:

$(function() {
    $('find-your-link').click(function(e) {
        e.preventDefault();
        $(this).parents('form').first().submit();
    });
});

That should submit the form without sending you to the GET URL.

ItsJason
  • 747
  • 1
  • 4
  • 20
1

try this:

HTML/C#:

<% 
using (Html.BeginForm(null,null,FormMethod.Post, new { id = "myform" })) 
{%>
    <p>
         <input type="submit" value="Choose" />
         .
         .
         .
         <%=Html.ActionLink("Save", "Index", null, new { id="action-save" })%>
    </p>
<% } %>

JS:

$(function() {

   $('#action-save').bind('click', function(event) {
       $('#myform').submit();     
       return false;
   });

});
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
0

An actionlink will by default always be a GET. You can't turn it into a POST out of the box.

Concerning you setup, the value from the dropdownlist should be in your FormCollection when you use the regular submit button.

An other way is to make the method parameters match the formvalues. For example if you have a dropdownlist named "foo", you could have a method Index(string foo), MVC will map the dropdownlist-value to that parameter if it has the same name. Do mind that the type of the formvalues should be able to be parsed into the type of the parameter. In your case if the valuetype of your dropdownlist is of type int, your method-parameter should be of typeint`. (you can't expect a form array of values to be parsed into a string type parameter).

Further I advise you to attribute the method with [HttpPost], more readable then [AcceptVerbs(HttpVerbs.Post)]

Incase you do want to use the ActionLink, you will have to start working on your javascript skills to eventually submit the formvalues.

TIP: If you set a break point on the first line of your POST-method, and you perform a watch on this you can get to the values being submitted by going through a few baseclasses and then find ValueProviders.

KyorCode
  • 1,477
  • 1
  • 22
  • 32
  • Thank you very much! I had a mistake in beginForm and now it's working fine with submitting by jquery and than must be return false how Jason says. – Petr Pražák Oct 19 '12 at 09:08