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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:11:46+00:00 2026-06-07T04:11:46+00:00

Right ok, I’m offically useless at this (see previous questions) Why, when I use

  • 0

Right ok, I’m offically useless at this (see previous questions)

Why, when I use the following do I get a lovely checkboxlistfor that works all nicely:
(using a extension method)

Model

public class My : BusinessCategory
{
        public class MyTypes
        {
            public int MyTypeId { get; set; }
            public string MyTypeName { get; set; }
            public bool? MyTypeValue { get; set; }
        }
        public List<MyTypes> MyTypeListModel { get; set; }
}

View

<fieldset>
    <legend>My Management</legend>
    <div style="text-align: left; padding-left: 47%;">
        @Html.CheckBoxListForListType(model => model.MyTypeListModel, (MultiSelectList)ViewBag.MyType, Model.ReviewId)
    </div>
    <p>
        <input type="submit" value="Continue" />
    </p>
</fieldset>

CheckBoxListForListType Extension (awful naming there I know)

public static MvcHtmlString CheckBoxListForListType<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, IEnumerable<TProperty>>> expression, MultiSelectList multiSelectList, Guid reviewId, object htmlAttributes = null)
        {
            //Derive property name for checkbox name
            MemberExpression body = expression.Body as MemberExpression;
            string propertyName = body.Member.Name;

            //Get currently select values from the ViewData model
            IEnumerable<TProperty> list = expression.Compile().Invoke(htmlHelper.ViewData.Model);

            //Convert selected value list to a List<string> for easy manipulation
            List<string> selectedValues = new List<string>();

            if (list != null)
            {
                selectedValues = new List<TProperty>(list).ConvertAll<string>(delegate(TProperty i) { return i.ToString(); });
            }

            //Create div
            TagBuilder divTag = new TagBuilder("div");
            divTag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

            //Add checkboxes
            foreach (SelectListItem item in multiSelectList)
            {
                divTag.InnerHtml += String.Format("<div><input type=\"checkbox\" name=\"{0}\" id=\"{0}_{1}\" " +
                                                    "value=\"{1}\" {2} /><label for=\"{0}_{1}\">{3}</label></div>",
                                                    propertyName,
                                                    item.Value,
                                                    GetValueFromDatabaseList(reviewId, propertyName, item.Text.ToLower()),
                                                    item.Text);
            }

            return MvcHtmlString.Create(divTag.ToString());
        }

Controller

    public ActionResult My(Guid id)
        {
            try
            {
                var model = Model(id);
                SetMyTypeList(model.My);
                ViewBag.MyType = new MultiSelectList(model.My.MyTypeListModel, "MyTypeValue", "MyTypeName");
                return View(model.My);
            }
            catch (Exception ex)
            {
                ExceptionHelper.WriteLog(ex);
                return RedirectToAction("Error");
            }
        }

private void SetMyTypeList(My model)
        {
            model.MyTypeListModel = new List<My.MyTypes>();
            model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 1, MyTypeName = GetName.GetDisplayName(model, m => m.Option1), MyTypeValue = model.Option1});
            model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 2, MyTypeName = GetName.GetDisplayName(model, m => m. Option2), MyTypeValue = model.Option2 });
            model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 3, MyTypeName = GetName.GetDisplayName(model, m => m. Option3), MyTypeValue = model.Option3});
            model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 4, MyTypeName = GetName.GetDisplayName(model, m => m.Option4), MyTypeValue = model.Option4});         
        }

Yet when I perform a HttpPost action result, MyTypeListModel ‘loses’ it’s properties i.e. I can see that, say two selections have been made but their names, values, id’s are not present.

 [HttpPost]
    public ActionResult My(Guid id, My model)
    {
        try
        {
            model.ReviewId = id;
            var sessionModel = Model(id); 
            sessionModel.My = model;
            MapCheckBoxListResultswithDatabaseBoolsMy(model);
            UpdateDb(Categories.catMy, sessionModel);

            return RedirectToAction("BusinessSummary", new { id = sessionModel.ReviewId });
        }
        catch (Exception ex)
        {
            ExceptionHelper.WriteLog(ex);
            return RedirectToAction("Error");
        }
    }

Do I have to recast to the type and if so where?

As per usual, many apologies for what appears to be a continuous thread of my ramblings. Hopefully things will start to click soon :'(

  • 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-06-07T04:11:47+00:00Added an answer on June 7, 2026 at 4:11 am

    The process of wiring up you action parameters is called model binding, and by default MVC interrogates the form, query string and route for the data to try and fill your type. In your case the problem will be that your form fields are not named in the default way that it expects for a list. You can find some information on binding lists in this article by Phil Haack. Keep in mind that you are looking for a subproperty so you need to prefix the type appropriately.

    One thing that I have found useful is to get the source code to ASP.NET MVC and then debug that locally to see what it is looking for when model binding.

    Don’t forget that if you don’t want to follow the normal conventions (although that is sensible) you can specify your own custom model binder for your type.

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

Sidebar

Related Questions

Right now I have a script that will get the last five files in
Right now I've programmed a method for use in a swing program that is
Right, i'm new to PHP and mySQL, but i've got a few questions that
Right now I use svn diff | vim - to get a colorized output.
Right now when I run this it keeps clicking on the same button every
Right now I have a function, in a class that is used to listen
Right guys, seeing as you were so helpful last time with my previous question
Right, so here the scenario: I've created a class called DiaryPage that inherites from
Right what I was trying to do is validate some input so that the
this is what i have right now Drawing an RSS feed into the php,

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.