MVC3 (Razor) Passing Model Data from View to Controller

14,958

Solution 1

I think that know what you want... but whit this code

@Html.ActionLink("Create Document", "createDoc", new { id = Model.ToList() })

your html is..

<a href="/test/createDoc?id=System.Collections.Generic.List%601%5BMvcMovie.Models.Movie%5D">Create Document</a>

and that's because is render the type not the data

Solutions

  1. define filter model to do the search again(the recommendation of jeremy-holovacs and mine) why ask to the server the same data again? because if someone share that link... you can imagine whats it's the result even inject fake data that your app will generate

  2. serialize data to json for example to forward it to the controller

Solution 2

Your model should not be IEnumerable<MvcMovie.Models.Movie> It should be a class, say SearchMovieModel, that has IEnumerable<MvcMovie.Models.Movie> Movies as one of its properties.

If you want a search model, something like this would be appropriate:

public class SearchMovieModel{
    public IEnumerable<MvcMovie.Models.Movie> Movies {get;set;}
    public string SearchString {get;set;}
}

you reference this model and its properties in your view and controller.

I guess I should add the method for parsing this in the controller.

On the first call to the view, the model does not exist. You need to create it in your controller:

public ActionResult Search(){
    var model = new SearchMovieModel();
    //you also need to instantiate the null objects unless you do that in the model's constructor
    model.Movies = new List<Movie>();
    return View(model);
}

To "reconvert" the POST data back to a model, you need to specify the model and method:

[HttpPost]
public ActionResult Search(SearchMovieModel model){
    if (ModelState.IsValid){
        //populate your IEnumerable<Movie> here.
        return View(model);
    }
    // the complex collection will not be parsed back into the model.  You will need to repopulate it.
    model.Movies = new List<Movie>();
    return View(model);
}
Share:
14,958
Jack88PD
Author by

Jack88PD

Updated on June 28, 2022

Comments

  • Jack88PD
    Jack88PD almost 2 years

    I need to know if there is a way to pass the Model (or a part of it, i.e. thereafter a search query) data of a View (Razor Engine) to a Controller.

    To explain in a better way what I have to do, that's the interested code:

    VIEW:

    @model IEnumerable<MvcMovie.Models.Movie>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    ...
    @foreach (var item in Model) { ...}
    ...
    
    @Html.ActionLink("Search", "SearchIndex")
    @Html.ActionLink("Create Document", "createDoc"/*, new { id = Model.ToList() }*/)
    

    I want to pass the

    @model IEnumerable<MvcMovie.Models.Movie>
    

    in the first line (= Model used in the foreach instruction) to the Controller "createDoc" to create a report document dynamically binded with the view.

    I tried everything: I tried to use a ViewData (VIEW: ViewData["data"]=Model , CONTROLLER List movies= ViewData["data"]), I similarly tried a TempData, I tried to pass the Model as routeValues in the ActionLink (as you can see: new{ id= Model.toList() }), but nothing worked.

    Is it even possible to do the thing I want to?

    Can anyone help me?