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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:00:57+00:00 2026-05-27T16:00:57+00:00

I have referred this question to learn how to post the checkbox selected values

  • 0

I have referred this question to learn how to post the checkbox selected values in the database.

However, I am not able to get my selected values on debug because of a NullReferenceException (see snapshots below) .

Here is my code:

Model:

 public class ProductModel
 {
    public string ProductId { get; set; }
    public string ProductName { get; set; }
    public bool Selected { get; set; }
    public string[] CheckedColumn { get; set; }
 }

View:

     @model IEnumerable<DemoApp.Models.ViewModels.ProductModel>

    @{
               ViewBag.Title = "CheckView";
     }

   <table> 
     <tr>
        <th>
          ProductId
        </th>
     <th>
        ProductName
     </th>
     <th>
        Selected
    </th>
        <th></th>
    </tr>

  @foreach (var item in Model) 
 {
   <tr>
      <td>
      @Html.DisplayFor(modelItem => item.ProductId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ProductName)
    </td>
    <td>
        @Html.CheckBoxFor(modelItem => item.Selected)
     </td>
   </tr> 
  }

  </table>
   @using (Html.BeginForm())  
   {
  <div>
  <input type="submit" name="AddResult" value="CheckView"/>
  </div>
    }

Controller:

    [HttpGet]
    public ActionResult CheckView()
    {
        DatabaseEntities db = new DatabaseEntities();

        var prodList = from b in db.SysUser3
                       select new ProductModel
                       {
                           ProductId = b.ProductId,
                           ProductName = b.ProductName,
                           Selected = b.SelectedProducts
                       };
        var p = prodList.ToList();
        return View(p);

    }

    [HttpPost]
    public ActionResult CheckView(FormCollection collection)
    {
        try
        {
            ProductModel model = new ProductModel();
            // Get all the selected checkboxlist, do db insertion
            model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }

    }

Here are some of the snapshots I took while running the project

After selecting the values from below figure

enter image description here

enter image description here

Am I doing something wrong in my View or Controller? can someone please help me ?

  • 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-27T16:00:58+00:00Added an answer on May 27, 2026 at 4:00 pm

    Your model is null because you are not passing anything from Controller ACtion. Your CheckView ActionMethod should be an action that generates the view. Then your “CheckView” button should call another HttpPost action that would look like your previous CheckView. Let me know if it helps in the comments.

    Action Result method that for the View:

        public ActionResult CheckView(ProductModel model)
        {
           return View("CheckView", model);
        }
    

    Action for button click.

    [HttpPost]
        public ActionResult TestView(FormCollection collection)
        {
            try
            {
                ProductModel model = new ProductModel();
                // Get all the selected checkboxlist, do db insertion
                model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
    
        }
    

    And in View something like this:

    @using (@Html.BeginForm("TestView", "Controller Name"))
    {
    //all your input here
    
    // submit button here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I referred to this somewhat similar question . However here the scenario is different:
I have referred this for creating error message tool tips, to be displayed continuously
I have asked this question on twitter as well the #clojure IRC channel, yet
I'm aware of several question on this forum relating to this. But I'm not
I'm not sure, if this is really a question or more an observation -
Referred this question first. But seems my context is different. I'll try to be
I have referred to the following question at: Using foreach loop to iterate through
I have referred all question of stackoverflow but i don't found exact answer of
I have heard of types being referred to as boxed in some languages. In
I have been reading about Quicksort and found that sometimes it' s referred to

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.