Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:15+00:00 2026-05-28T05:42:15+00:00

Fairly new to MVC3 and I am doing a simple project for testing purposes.

  • 0

Fairly new to MVC3 and I am doing a simple project for testing purposes. So I have encountered a slight problem which I would like your help about.

At the moment I have this code :-

<p>
@if (UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}

My question is, how can I populate the boolean value UserCanCreate from my controller? I tried creating a property in the Controller, but the View still does not see this.

Is there maybe a better way to do this?

Thanks for your help

UPDATED with ViewModel

@model IEnumerable<DBName.ViewModels.ProfileData>


@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.ActionLink("Details", "Details", new { id=item.FantaTeamID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.FantaTeamID })
    </td>
</tr>

}

How can I replace this code now? from where to get item.UserName?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:42:15+00:00Added an answer on May 28, 2026 at 5:42 am

    There are at least two different ways you can do it.

    First one (better one) is using a Model to pass data. For that you should create a Model class:

    //Model.cs
    public class MyModel
    {
        public bool UserCanCreate { get; set;}
    }
    

    and specify it at the beginning of your View (use typed view):

    @model %Your_model_namespace%.MyModel
    
    ...
    
    <p>
    @if (Model.UserCanCreate)
    {
        @Html.ActionLink("Create New", "Create")
    }
    

    And, of course, in your controller action do the following:

    public ActionResult YourAction()
    {
        ...
        MyModel model = new MyModel();
        model.UserCanCreate = ... ; // Determine if link shall be visible here
        ...
        return View(model);
    }
    

    Second (faster one, although not recommended): use ViewData collection to pass values.

    For example, in your controller action do:

    public ActionResult YourAction()
    {
        ...
        ViewData["UserCanCreate"] = ... ; // Determine if link shall be visible here
        ...
        return View();
    }
    

    and your view will look like:

    ...
    
    <p>
    @if ((bool)ViewData["UserCanCreate"])
    {
        @Html.ActionLink("Create New", "Create")
    }
    

    Also important: if you’re trying to secure some actions that way then do not forget to add some checks for permissions inside “Create” action code – otherwise any user will be able to execute it by just entering corresponding URL in browser address bar – even if the ActionLink is not present.

    UPDATE

    As for your comment – you seem to be mixing domain models with view models. Domain models are the ones that represent your database entities, while view models are the ones that should be used for presentation. Generally it’s controller’s job to convert objects from domain to view models and vice versa.

    In your case EF models are domain models.

    For ViewModel you should create a separate class which will aggregate your domain model instance and any additional properties you want – like UserCanCreate.

    Example model class:

    public class UsersViewModel
    {
        public IEnumerable<DBName.Models.Users> Users { get; set; }
        public bool UserCanCreate { get; set; }
        ... // Any other additional properties you may require
    }
    

    In your controller it will be:

    public ActionResult YourAction()
    {
        ...
        UsersViewModel model = new UsersViewModel();
        model.Users = ... ; // Get your IEnumerable<DBName.Models.Users> collection from DB and assign it to model property here
        model.UserCanCreate = ... ; // Determine if link shall be visible here
        ...
        return View(model);
    }
    

    and in view:

    @model %Your_model_namespace%.UsersViewModel
    
    ... // use Model.Users wherever you need your IEnumerable<DBName.Models.Users>
    
    <p>
    @if (Model.UserCanCreate)
    {
        @Html.ActionLink("Create New", "Create")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to the world of versioning but would like to introduce Subversion
I have a fairly simple Action in an MVC3 application that should render an
I am fairly new to unit testing. I am building an ASP.NET MVC3 application
In fairly new to MVC and I would like to use a session. I
I am fairly new to Emacs and I have been trying to figure out
Fairly new to socket programming, but I've been assigned with a whopper of project.
Being fairly new to C++ I have a question bascially concerning the g++ compiler
I am writing a fairly simple MVC3 application that allows a user to retrieve
I'm fairly new to asp.net MVC3. I followed the link below http://blogs.msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspx it all
Fairly new to sqlite (and sql). For each row in a table, I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.