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 3724242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:34:11+00:00 2026-05-19T06:34:11+00:00

I have an ASP.NET MVC 3 (Razor) website, and a (simplified) model called Review

  • 0

I have an ASP.NET MVC 3 (Razor) website, and a (simplified) model called Review:

public class Review
{
   public int ReviewId { get; set; }
   public bool RecommendationOne
   {
       // hook property - gets/set values in the ICollection
   }
   public bool RecommendationTwo { // etc }
   public ICollection<Recommendation> Recommendations { get; set; }
}

Recommendation is as follows:

public class Recommendation
{
   public byte RecommendationTypeId
}

I also have an enum called RecommendationType, which i use to map the above recommendation to. (based on RecommendationTypeId).

So to summarize – a single Review has many Recommendations, and each of those Recommendations map to a particular enum type, i expose hook properties to simplify model-binding/code.

So, onto the View:

@Html.EditorFor(model => model.Recommendations, "Recommendations")

Pretty simple.

Now, for the editor template, i want to display a checkbox for each possible RecommendationType (enum), and if the model has that recommendation (e.g on edit view), i check the box.

Here’s what i have:

@model IEnumerable<xxxx.DomainModel.Core.Posts.Recommendation>
@using xxxx.DomainModel.Core.Posts;

@{
    Layout = null;
}

<table>
    @foreach (var rec in Enum.GetValues(typeof(RecommendationType)).Cast<RecommendationType>())
    {
        <tr>
            <td>
                @* If review contains this recommendation, check the box *@
                @if (Model != null && Model.Any(x => x.RecommendationTypeId == (byte)rec))
                {
                    @* How do i create a (checked) checkbox here? *@
                }
                else
                {
                    @* How do i created a checkbox here? *@
                }

                @rec.ToDescription()
            </td>
        </tr>
    }
</table>

As the comments suggest – i don’t know how to use @Html.CheckBoxFor. Usually that takes an expression based on the model, but i’m how sure how to bind to the hook property based on the currently looped enum value. E.g it needs to dynamically do @Html.CheckBoxFor(x => x.RecommendationOne), @Html.CheckBoxFor(x => x.RecommendationTwo), etc.

The current solution i have (which works), involves manually constructing the <input> (including hidden fields).

But as i’m just getting the hang of editor templates, hoping someone with experience can point me in a “strongly-typed” direction.

Or is there a nicer way (HTML Helper) i can do this?

  • 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-19T06:34:12+00:00Added an answer on May 19, 2026 at 6:34 am

    I would start by introducing a proper view model for the scenario:

    public enum RecommendationType { One, Two, Three }
    
    public class ReviewViewModel
    {
        public IEnumerable<RecommendationViewModel> Recommendations { get; set; }
    }
    
    public class RecommendationViewModel
    {
        public RecommendationType RecommendationType { get; set; }
        public bool IsChecked { get; set; }
    }
    

    Then the controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // TODO: query the repository to fetch your model
            // and use AutoMapper to map between it and the 
            // corresponding view model so that you have a true/false
            // for each enum value
            var model = new ReviewViewModel
            {
                Recommendations = new[]
                {
                    new RecommendationViewModel { 
                        RecommendationType = RecommendationType.One, 
                        IsChecked = false 
                    },
                    new RecommendationViewModel { 
                        RecommendationType = RecommendationType.Two, 
                        IsChecked = true 
                    },
                    new RecommendationViewModel { 
                        RecommendationType = RecommendationType.Three, 
                        IsChecked = true 
                    },
                }
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(ReviewViewModel model)
        {
            // Here you will get for each enum value the corresponding
            // checked value
            // TODO: Use AutoMapper to map back to your model and persist
            // using a repository
            return RedirectToAction("Success");
        }
    }
    

    and the corresponding view (~/Views/Home/Index.cshtml):

    @model YourAppName.Models.ReviewViewModel
    
    @{
        ViewBag.Title = "Index";
    }
    
    @using (Html.BeginForm())
    {
        @Html.EditorFor(model => model.Recommendations)
        <input type="submit" value="Go" />
    }
    

    and finally the editor template (~/Views/Home/EditorTemplates/RecommendationViewModel.cshtml)

    @model YourAppName.Models.RecommendationViewModel
    <div>
        @Html.HiddenFor(x => x.RecommendationType)
        @Model.RecommendationType 
        @Html.CheckBoxFor(x => x.IsChecked)
    </div>
    

    Now the view code is cleaned as it should. No ifs, no loops, no LINQ, no reflection, this is the responsibility of the controller/mapper layer. So every time you find yourself writing some advanced C# logic in your view I would recommend you rethinking your view models and adapt them as necessary. That’s what view models are intended for: to be as close as possible to the view logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET MVC Razor And Data Annotation validators My model: public class
I have the following asp.net mvc 3 razor code, where item is my model,
I am developing an ASP.Net MVC 3 Web application using Razor Views. I have
I have an ASP.NET MVC website where I've established a system of Permissions. There
I am working on asp.net MVC 3 application. I have created a Razor view
I have an ASP.NET MVC website which has a particular javascript file that needs
I have read Scott Guthrie's Blog - ASP.NET MVC 3: New @model keyword in
I have a ASP.Net MVC app with Razor. I try to access a collection
I have a partial view in an ASP.NET MVC app: @Html.Partial(_Comments, Model) I want
I have a new ASP.NET MVC 3 project, and MVC and Razor are all

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.