Question

I have a long form that's been split up into separate pages using partial views. I want to save the data entered into one partial view form when the user clicks to go to the next patial view form. Then have it so that when they come back to the previous view then the data entered will be there.

So what i need is to post the data to a controller. Although what I am using for the partial view is:

@Ajax.ActionLink("Job Information", "JobInformation", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "jobForm"})

When they click the action link it takes them to the next page (partial view)

So how can i submit the form when they click the action link?

Was it helpful?

Solution

how can i submit the form when they click the action link?

i need is to post the data to a controller.

Instead of using AJAX ActionLink use AJAX BeginForm as shown below. Lets say you have a model like below, to populate a dropdownlist.

public class DDLModel
{
    public List<SelectListItem> Items { get; set; }
    public string SelectedValue { get; set; }
}

Then you have controller to display this model -

@model MVC.Controllers.DDLModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>


@using (Ajax.BeginForm("Submit", "Ajax", new AjaxOptions { UpdateTargetId = "SellerWebSettings" }, new { id = "form1" }))
{
    @Html.DropDownListFor(m => m.SelectedValue, Model.Items, "DDL")
    <input type="submit" value="Click" />
}

<div id="SellerWebSettings">
</div>

In the above form, you have a AJAX BeginForm() which would make a AJAX all to another controller action which will return a PartialView. The model will also be forwarded to the partial view, as shown below -

public ActionResult Submit(DDLModel model)
{
    return PartialView("MyPartial",model);
}

And the partial view is as follows -

@model MVC.Controllers.DDLModel

<div>
    @DateTime.Now - @Model.SelectedValue
</div>

So when we select the Dropdownlist item and click on the button, we have following output -

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top