user2471435
user2471435

Reputation: 1674

MVC Model Binding Issue always NULL

I am trying to add a new MVC ActionLink that when pressed goes to an action and gets bound to the input parameter but it doesn’t seem to be working

I Have this:

@Html.ActionLink("OPTIN","Optin", "Admin", new {id = Model.Publications[i].ID}) 

And then the Optin method on the Admin controller:

 public ActionResult OptIn(int? id)
        {
            if (id.HasValue)
            {
                var temp = id.Value;
            }

I want the id to get mapped to the input parameter but it is always NULL and Model.Publications[i].ID is always 5. What am I doing wrong? Do I have to make ID part of a model?

Upvotes: 0

Views: 143

Answers (1)

krillgar
krillgar

Reputation: 12815

The way that you have your ActionLink method set up is wrong. I'm GUESSING the override you're using is this one. However, you'll want to be using is string, string, string, object, object.

By the URL in the comment on your question, the ID that you're setting is the HTML attribute, not a part of the URL.

If you change your call to the following, it should give you what you need:

@Html.ActionLink("OPTIN","Optin", "Admin", new {id = Model.Publications[i].ID}, new { }) 

Upvotes: 1

Related Questions