Question

I am trying to use an ASP MVC3 action link to navigate to another view (same controller). The view is attached to a model that uses a compound key for its primary key. Below is the action link as it's written on the view

@Html.ActionLink("Edit Agent", "AgentEdit", "BankListMasterController", 
                                                new { @agentId = int.Parse(item.AgentId), @id = item.ID})

However when this is rendered it renders as the following

http://localhost:2574/BankListMaster/AgentEdit?Length=24

Which obviously throws an error:

The parameters dictionary contains a null entry for parameter 'agentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult AgentEdit(Int32, Int32)' in 'Monet.Controllers.BankListMasterController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

Here is the controller method for good measure:

    public ViewResult AgentEdit(int agentId, int id)
    {
        string compare = agentId.ToString();

        BankListAgentId agent = (from c in db.BankListAgentId
                                 where c.ID == id &&
                                       c.AgentId.Equals(compare)
                                 select c).Single();

        return View("AgentEdit", agent);
    }
Was it helpful?

Solution

@Html.ActionLink("Edit Agent", "AgentEdit", "BankListMasterController", 
                                                new { agentId = int.Parse(item.AgentId), id = item.ID}, null)

That should do the trick

And rationale is: as per http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.108).aspx

You won't find there method with (HtmlHelper, string, string, string, object) there's however (HtmlHelper, string, string, string, object, object) where the second last object is route values and the last are html attributes.

OTHER TIPS

Based on the parameters you have provided, the wrong ActionLink is being called.

The problem is that it is "attempting to serialize a string object"

Here is the canonical answer to the "'Length' parameter in link" question: Why does Html.ActionLink render "?Length=4"

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