ASP.NET MVC - Trouble passing model in Html.ActionLink routeValues

后端 未结 6 1458
北恋
北恋 2020-12-09 19:25

My View looks like this:

<%@ Control Language=\"C#\" 
    Inherits=\"System.Web.Mvc.ViewUserControl

        
6条回答
  •  攒了一身酷
    2020-12-09 19:54

    You can't pass complex objects:

    new
    {
        model = Model,
        sortBy = "EffectiveStartDate",
    },
    

    model = Model makes no sense and cannot be sent using GET. You might need to use a form with an editor template and/or hidden fields to send all the model properties. Remember only scalar values can be sent in the query string (key1=value1&key2=value2...). Another alternative that comes to mind is to send only the ID:

    new
    {
        modelId = Model.Id,
        sortBy = "EffectiveStartDate",
    },
    

    and in your controller action fetch the model given this id from your data store:

    public ActionResult SortDetails(int modelId, String sortBy)
    {
        var model = repository.GetModel(modelId);
        ...
    }
    

    Of course this is only true if the user is not supposed to edit the model properties in a form. Depends on your scenario.

    And for the sake of completeness let me expose another option: use the Html.Serialize helper from MVC Futures to serialize the entire model into a hidden field which could be passed back to the controller action and deserialized there.

提交回复
热议问题