Post form fields with Ajax.ActionLink

2019-06-28 02:28发布

问题:

I have a View in my MVC 3 application that lets the user enter the shipping address and place the order. The form fields that I show on the view are exactly the same as user profile fields. So I want to consider a link for the user to save the data they have entered into their profile too. Fields like first name, last name, address and so on. I want to use Ajax.ActionLink, but the problem is I don't know how to send the form fields to the Action that saves the data. What I have is:

@Ajax.ActionLink("Save into profile", "SaveAccountProfile", new{ address=????}, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnSuccess = "alert('Account profile was successfully updated.')" })

What would I post as the route data?

回答1:

You should use an Ajax.BeginForm in this case. This way form field values will automatically be posted to the server.

@using (Ajax.BeginForm("SaveAccountProfile", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnSuccess = "alert('Account profile was successfully updated.')" }))
{
    ... some input fields
    <input type="submit" value="Save into profile" />   
}