skip to Main Content

I’ve written this code in View. And the picture is the output of it in Chrome Inspect.

@Html.CheckBox("KYCComplete", new { @name = "KYC", @id = "KYC" })

    <label class="form-check-label" for="KYC">
        KYC Complete ?
        <button id="Submit">KYC Complete ?</button>
    </label>

Output in Chrome Inspect

In my controller I’m using this HttpPost to use checkbox an filter:

[HttpPost]
public ActionResult Index(bool KYCComplete)
{
        if (KYCComplete)
        {
            List<BankAccount> b= db.BankAccounts.Where(p => p.KYCComplete == true).OrderBy(b => b.City).ToList();
            return View(b);
        }

        return RedirectToAction("Index");
}

Everything works fine, up to this point. Only name property is not overridden.

Well, I want to change the name property of the checkbox to be "KYC" from "KYCComplete".

So, firstly I look for Is there any ways to override HTML helpers. I found in several websites it’s not possible to override those.

Now I tried with writing simple HTML checkbox and I’m getting an error.

Server Error in ‘/’ Application.

The parameters dictionary contains a null entry for parameter
‘KYCComplete’ of non-nullable type ‘System.Boolean’ for method
‘System.Web.Mvc.ActionResult Index(Boolean)’ in
‘BankAccountsMgmt.Controllers.BankAccountsController’. An optional
parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

So how can I change the name property of this checkbox to "KYC" and bind its input, to filter the desired result.


Describing the question In a Nutshell

As, you have seen the output of the view of this checkbox has name property "KYCComplete".

I’ve requirement to change to "KYC", and HttpPost should work along with it, without effecting domain model.


Incase extra info. is Required

model:

namespace BankAccountsMgmt.Models
{
public class BankAccount
    {
     ...
        [Display(Name = "KYC Complete?")]
        public bool KYCComplete { get; set; }
     ... 
     }
}

controller:

using BankAccountsMgmt.Data;
using BankAccountsMgmt.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace BankAccountsMgmt.Controllers
{
    [CustomFilters.CustomExceptionFilter]
    public class BankAccountsController : Controller
    {
        BankAccountDBContext db = new BankAccountDBContext();
        // GET
        public ActionResult Index()
        {
            //Implement your code 
            List<BankAccount> bank = db.BankAccounts.OrderBy(b=>b.City).ToList();
            return View(bank);
        }
        //Implement other action methods

        [HttpPost]
        public ActionResult Index(bool KYCComplete)
        {
            if (KYCComplete)
            {
                List<BankAccount> AccKYC = db.BankAccounts.Where(p => p.KYCComplete == true).OrderBy(b => b.City).ToList();
                return View(AccKYC);
            }
            return RedirectToAction("Index");
        }


        public ActionResult AddBankAccount()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddBankAccount(BankAccount bankAccount)
        {
            if (ModelState.IsValid)
            {
                bankAccount.CalculateInterest();
                db.BankAccounts.Add(bankAccount);
                db.SaveChanges();
                ViewBag.Message = "Bank Account added successfully!";
                return View("Details", bankAccount);
            }

            return View(bankAccount);
        }


    }
}

full view:

@model List<BankAccountsMgmt.Models.BankAccount>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Bank Accounts List</h2>




@using (Html.BeginForm("Index", "BankAccounts"))
{
    <div class="form-check col-md-offset-8" align="center">
        @Html.CheckBox("KYCComplete", new { @name = "KYC", @id = "KYC" })

        <label class="form-check-label" for="KYC">
            KYC Complete ?
            <button id="Submit">KYC Complete ?</button>
        </label>
    </div>
    <table class="table">
        <tr>
            <th>
                Account Holder Name
            </th>
            <th>
                PAN Number
            </th>
            <th>
                City
            </th>
            <th>
                Gender
            </th>

            <th>
                Amount
            </th>
            <th>
                Interest Upto 30 Aug
            </th>
            <th>
                Opening Date
            </th>
            <th>
                KYC Complete?
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.AccountHolderName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PAN)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.City)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Gender)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Amount)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Interest)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.OpeningDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.KYCComplete)
                </td>

            </tr>
        }

    </table>
    <span id="total" class="form-check col-md-offset-6" align="center"><b>Interest Total = </b>@Model.Sum(model => model.Interest).ToString("#.##") </span>
}


2

Answers


  1. You could try Html Checkbox instead of using html helper method:

    Example:

    <input type="checkbox" id="KYC" name="KYC">
      <label for="KYC"> KYC Complete</label>
     
    
    Login or Signup to reply.
  2. The name of your form control must match the name of the parameter to your action method for model binding to work.

    If you change the name of the checkbox to "KYC", but leave the action parameter as "KYCComplete", you will get the exception mentioned in your question.

    Change the name in both places.

    [HttpPost]
    public ActionResult Index(bool KYC)
    {
        if (KYC)
        {
            List<BankAccount> b= db.BankAccounts.Where(p => p.KYCComplete == true).OrderBy(b => b.City).ToList();
            return View(b);
        }
    
        return RedirectToAction(nameof(Index));
    }
    
    @Html.CheckBox("KYC", new { @id = "KYC" })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search