saisneImane
saisneImane

Reputation: 1

asp.net mvc3, c#

Is there an easy way to pass an object from my controller to view to controller?

I tried ViewData["compaignId"] but that didn't work.

Controller "Content" :

ViewData["CompaignId"] = id;

View "Content" :

<div>
    @Html.ActionLink("Go To Team Creation", "Team", ViewData["CompaignId"])
</div>

Controller "Team":

public ActionResult Team(long CompaignId)
{
    ....
}

Thanks,

Upvotes: 0

Views: 88

Answers (3)

dknaack
dknaack

Reputation: 60438

In general you should use a ViewModel for things like that. But you can use the ViewBag too.

ViewBag.CompaignId = id;

Upvotes: 1

Nildarar
Nildarar

Reputation: 812

U can use:

Controller "Content" :

ViewBag.CompaignId = id;

View "Content" :

<div>
    @Html.ActionLink("Go To Team Creation", "Team", new 
    { 
        CompaignId = (long)ViewBag.CompaignId 
    })
</div>

Controller "Team":

public ActionResult Team(long CompaignId)
{
    ....
}

Upvotes: 0

archil
archil

Reputation: 39491

You should take a look at this overload

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues
)

And you can pass anonymous object as last parameter

<div>
    @Html.ActionLink("Go To Team Creation", "Team", new { campaignId = (int)ViewData["CompaignId"] })
</div>

Upvotes: 0

Related Questions