简体   繁体   English

ASP.NET MVC 中的复选框,不使用 HTML 辅助方法

[英]Checkbox in ASP.NET MVC without using HTML helper method

I've written this code in View.我已经在 View 中编写了这段代码。 And the picture is the output of it in Chrome Inspect.图片是它在 Chrome Inspect 中的 output。

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

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

Chrome 检查中的输出

In my controller I'm using this HttpPost to use checkbox an filter:在我的 controller 中,我使用这个HttpPost来使用复选框过滤器:

[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.只有 name 属性没有被覆盖。

Well, I want to change the name property of the checkbox to be "KYC" from "KYCComplete".好吧,我想将复选框的名称属性从“KYCComplete”更改为“KYC”。

So, firstly I look for Is there any ways to override HTML helpers.所以,首先我寻找是否有任何方法可以覆盖 HTML 助手。 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.现在我尝试编写简单的 HTML 复选框,但出现错误。

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'.参数字典包含一个 null 条目,用于方法 'System.Web.Mvc.ActionResult Index(Boolean)' in 'BankAccountsMgmt.Controllers.Bank 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.那么如何将此复选框的名称属性更改为“KYC”并绑定其输入,以过滤所需的结果。


Describing the question In a Nutshell简述问题

As, you have seen the output of the view of this checkbox has name property "KYCComplete".如您所见,此复选框视图的 output 具有名称属性“KYCComplete”。

I've requirement to change to "KYC", and HttpPost should work along with it, without effecting domain model.我需要更改为“KYC”,并且 HttpPost 应该与它一起工作,而不影响域 model。


Incase extra info.以防额外信息。 is Required是必须的

model: model:

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

controller: 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>
}


You could try Html Checkbox instead of using html helper method:您可以尝试 Html Checkbox 而不是使用 html 辅助方法:

Example:例子:

<input type="checkbox" id="KYC" name="KYC">
  <label for="KYC"> KYC Complete</label>
 

The name of your form control must match the name of the parameter to your action method for model binding to work.您的表单控件的名称必须与您的操作方法的参数名称匹配,model 绑定才能工作。

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.如果您将复选框的名称更改为“KYC”,但将操作参数保留为“KYCComplete”,您将收到问题中提到的异常。

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" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM