charlie_cat
charlie_cat

Reputation: 1850

How to make use of Viewbag in my View

I dont understand, I have my in my Controller:

[HttpGet]
public ActionResult Detail(int userId)
{
  var user = ZincService.GetUserForId(userId);
  if (user != null)
  { 
      ViewBag.user = userId;
      ViewBag.email = user.Email;
      ViewBag.title = user.JobTitle;
      ViewBag.node = user.Node;
  }
  return View(user);
}

then my view, Detail.aspx

<div id="user-details-view">
<div>
  Title:
</div>
<div>
   <%: Model.JobTitle %>
   <%: Model.News %>
   <%: Model.Node %>
</div>
<div>
   <%: Html.ActionLink("Change Email Address", "ChangeEmailAddress", new { @id = Model.UserId })%>
</div>
</div>

when I run my app i get an error:

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Areas/Admin/Views/User/Detail.aspx

I dont understand? Is it because of syntax errors?

Upvotes: 0

Views: 560

Answers (4)

codingbiz
codingbiz

Reputation: 26396

Replace the Model. with ViewBag.

  <%: ViewBag.title %>
   <%: ViewBag.email %>
   <%: ViewBag.node %>

and also change this

<%: Html.ActionLink("Change Email Address", "ChangeEmailAddress", new { id = ViewBag.user })%>

Upvotes: 1

Xordal
Xordal

Reputation: 1369

You must use that sintax:

<%: ViewBag.email %>
<%: ViewBag.title %>
<%: ViewBag.node %>

But would be better if you use Model:

public class UserInfo
{
    public int UserId { get; set; }
    public string Email { get; set; }
    public string Title { get; set; }
    public NodeType Node { get; set; }
}

[HttpGet]
public ActionResult Detail( int userId )
{
    var data = ZincService.GetUserForId( userId );
    var user = new UserInfo();
    if ( data != null )
    {
        user.UserId = userId;
        user.Email = data.Email;
        user.Title = data.JobTitle;
        user.Node = data.Node;
    }
    return View( user );
}

In view (MVC3) with razor sintax:

@model UserInfo
<div id="user-details-view">
<div>
  Title:
</div>
<div>
   @Model.Title
   @Model.Node
</div>
<div>
   @Html.ActionLink("Change Email Address", "ChangeEmailAddress", new { @id = Model.UserId })
</div>
</div>

Upvotes: 0

Tsasken
Tsasken

Reputation: 689

Previous posts are correct as in logic but you assigned the viewbag names in your controller differently. It should be:

<div id="user-details-view">
    <div>
       Title:
    </div>
    <div>
        <%: ViewBag.email %>
        <%: ViewBag.title %>
        <%: ViewBag.node %>
    </div>
    <div>
        <%: Html.ActionLink("Change Email Address", "ChangeEmailAddress", new { @id = ViewBag.user })%>
    </div>
</div>

Hope it helps..

Upvotes: 3

Irakli Lekishvili
Irakli Lekishvili

Reputation: 34198

It must be like this

 <%: ViewBag.JobTitle %>
   <%: ViewBag.News %>
   <%: ViewBag.Node %>

Upvotes: 1

Related Questions