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

  • Home
  • SEARCH
  • 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 6539497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:51:57+00:00 2026-05-25T10:51:57+00:00

I am using pubs database and basically I show a list of authors on

  • 0

I am using pubs database and basically I show a list of authors on one page and there is edit link. On click of edit link, one page gets showed which has author’s information and checkboxlist with checkmark besides the titles that the particular author have written.

This is my code till now:

Action methods of HomeController:

public ActionResult Edit(string id)
{
    author author = db.authors.Include("titleauthors").Where(a => a.au_id == id).Single();

    List<TitleViewModel> titleViewModelList = db.titles.Select<title, TitleViewModel>(title =>
        new TitleViewModel { IsChecked = false, Title = title.title1, title_id = title.title_id }).ToList();

    foreach (var item in author.titleauthors)
    {
        TitleViewModel titleViewModel = titleViewModelList.Where(model => model.title_id == item.title_id).SingleOrDefault();
        if (titleViewModel != null)
            titleViewModel.IsChecked = true;
    }

    AuthorViewModel AuthorViewModel = new AuthorViewModel
    {
        Author = author,
        TitleViewModels = titleViewModelList
    };

    return View(AuthorViewModel);
}

[HttpPost]
public ActionResult Edit(AuthorViewModel AuthorViewModel)
{

    return RedirectToAction("Index");
}

Edit.cshtml:

@model MVCCheckboxList.ViewModels.AuthorViewModel
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Edit</h2>
<fieldset>
    @using (Html.BeginForm())
    {
        <legend>Edit author</legend>
        <table>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Author.au_fname)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Author.au_fname)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Author.au_lname)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Author.au_lname)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Author.address)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Author.address)
                </td>
            </tr>
            <tr>
                <td valign="top">
                    Titles
                </td>
                <td>
                    @Html.EditorFor(model => model.TitleViewModels, "TitleViewModels")
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <input type="submit" value="Save" />
                    </p>
                </td>
            </tr>
        </table>
    }
</fieldset>

This is my AuthorViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCheckboxList.ViewModels
{
    public class AuthorViewModel
    {
        public List<TitleViewModel> TitleViewModels { get; set; }
        public author Author { get; set; }
    }
}

and this is my TitleViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCheckboxList.ViewModels
{
    public class TitleViewModel
    {
        public string title_id { get; set; }
        public bool IsChecked { get; set; }
        public string Title { get; set; }
    }
}

The information gets properly displayed in Edit page. But when I click on submit, the Author property in AuthorViewModel correctly holds all the data but the List<TitleViewModel> in AuthorViewModel is null. Can anybody spot the mistake why is this happening?

This is my EditorTemplate(TitleViewModels.cshtml):

@model IEnumerable<MVCCheckboxList.ViewModels.TitleViewModel>
<table>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.HiddenFor(modelItem => item.title_id)
        </td>
        <td>
            @Html.CheckBoxFor(modelItem => item.IsChecked)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
    </tr>
}

</table>
  • 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-25T10:51:58+00:00Added an answer on May 25, 2026 at 10:51 am

    Here you find more informations about list binding.

    Just change the name of your EditorTemplate to TitleViewModel.cshtml and paste that code:

    @model MVCCheckboxList.ViewModels.TitleViewModel
    
    <td>
        @Html.HiddenFor(modelItem => Model.title_id)
    </td>
    <td>
        @Html.CheckBoxFor(modelItem => Model.IsChecked)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.Title)
    </td>
    

    In Edit.cshtml use below code to generate editor for each element from your list

    <table>
    @for (int i = 0; i < Model.TitleViewModels.Count; i++) {
        @Html.EditorFor(m => Model.TitleViewModels[i])
    }
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Visual SourceSafe's Open SourceSafe Database... command, there is an option at the bottom
This might seem a pretty simple question; I'm using the Pubs database with the
Using Microsoft SQL Server 2005, is there any way to see when a table
Using the Qt library, is there a way to automatically update a QDateTimeEdit using
Using System.Diagnostics.EventLog .NET type one can programmatically create logs into the Event Viewer application.
One of the problems I am having with c# is that there seems to
Using the following as an example (with $db being a previously created database connection
Environment: SQL Server 2005/2008, pubs database I have inserted into a table variable a
Using pubs If I want to join using query syntax I would do this.
Using JavaParser I can get a list of my methods and fields using: //List

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.