Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax.ActionLink post entire model from a view?

I have a view that is strongly typed to a ViewModel. Is it possible to pass all of the data from a model in the view, back to a controller action? Something like this?

@Ajax.ActionLink("(Export to Excel)", "ExportCsv", "SurveyResponse", new {  
ResultsViewModel = Model }, new AjaxOptions {HttpMethod = "POST"})

And then collect the data from ResultsViewModel as a parameter in another controller

public ActionResult ExportCsv(ResultsViewModel resultsviewmodel)
{

}
like image 830
user547794 Avatar asked Oct 10 '12 21:10

user547794


People also ask

How can we navigate from one view to another using Action Link?

ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

How can we call post method using ActionLink in MVC?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.

How will you navigate from one view to another view in MVC explain with a hyperlink example?

Call the appropriate /controller/action in your respective button click handlers. In your case for the register button handler direct it to /home/register. Have a view for your register functionality. In the register action of your home controller return the view you want to show.


2 Answers

No, you cannot pass entire view model like this in an action link. You could pass only the id of this model and then retrieve the actual model using this id from wherever you retrieved it initially:

@Ajax.ActionLink(
    "(Export to Excel)", 
    "ExportCsv", 
    "SurveyResponse", 
    new { id = Model.Id }, 
    new AjaxOptions { HttpMethod = "POST" }
)

As an alternative you could serialize the model as a javascript literal and then send it as a JSON data with the AJAX request:

@Html.ActionLink(
    "(Export to Excel)", 
    "ExportCsv", 
    "SurveyResponse", 
    null, 
    new { @class = "exportCsv" }
)
<script type="text/javascript">
    $('.exportCsv').click(function() {
        var model = @Html.Raw(Json.Encode(Model));
        $.ajax({
            url: this.href,
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(model),
            success: function(result) {

            }
        });
        return false;
    });
</script>
like image 147
Darin Dimitrov Avatar answered Oct 12 '22 23:10

Darin Dimitrov


Managed to make below works,

@Ajax.ActionLink("(Export to Excel)", "ExportCsv", "SurveyResponse", 
new { Model.F1, Model.F2, Model.OtherFields }, new AjaxOptions {HttpMethod = "POST"})

Controller

[HttpPost]
public ActionResult ExportCsv(ResultsViewModel resultsviewmodel)
{

}

This is a http post, but the data in not in "form data", it's encoded in request's URL (but not a http get).

Looks like MVC automatically converts the individual fields into a single model.

URL has a length limits, large model may fail.

like image 29
Rm558 Avatar answered Oct 13 '22 01:10

Rm558