【问题标题】:Save checkbox data in SQL Server using MVC使用 MVC 在 SQL Server 中保存复选框数据
【发布时间】:2019-06-27 06:41:34
【问题描述】:

我使用 MVC 并希望将复选框数据以布尔形式保存在 sql server 中。 当我尝试将 Razor 视图用于复选框模型时,错误出现在客户表单模型中。

错误是:

CS0266:无法隐式转换类型“bool?” '布尔'。存在显式转换(您是否缺少演员表?)

控制器代码:

public ActionResult SaveRecord(CustomerCount cc)
{
    try
    {
        CustomerCounterDBEntities1 dbs = new CustomerCounterDBEntities1();
        List<CustomerInfo> infos = dbs.CustomerInfoes.ToList();
        ViewBag.CustomerInfoList = new SelectList(infos, "Name", "Mobile");
        CustomerInfo ct = new CustomerInfo();
        ct.CustomerID = cc.CustomerID;
        ct.Name = cc.Name;
        ct.Mobile = cc.Mobile;
        ct.Email = cc.Email;
        ct.Comments = cc.Comments;
        ct.Western_Union = cc.Western_Union;
        ct.Ria = cc.Ria;
        ct.Money_Gram = cc.Money_Gram;
        ct.Intel = cc.Intel;
        ct.JazzCash = cc.JazzCash;
        ct.Contact = cc.Contact;
        ct.No_Business = cc.No_Business;
        dbs.CustomerInfoes.Add(ct);
        dbs.SaveChanges();
        int CustomerID = ct.CustomerID;
        return RedirectToAction("Index");
    }

型号代码:

namespace Customer_Counter.Models
{
    public class CustomerCount
    {
        [Key]
        public int CustomerID { get; set; }

        public string Name { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public string Comments { get; set; }
        public Boolean Western_Union { get; set; }
        public Boolean Ria { get; set; }
        public Boolean Money_Gram { get; set; }
        public Boolean Intel { get; set; }
        public Boolean JazzCash { get; set; }
        public Boolean Contact { get; set; }
        public Boolean No_Business { get; set; }
    }
}

客户信息:

namespace Customer_Counter.Models
{
    using System;
    using System.Collections.Generic;

    public partial class CustomerInfo
    {
        public int CustomerID { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public string Comments { get; set; }
        public Nullable<bool> Western_Union { get; set; }
        public Nullable<bool> Ria { get; set; }
        public Nullable<bool> Money_Gram { get; set; }
        public Nullable<bool> Intel { get; set; }
        public Nullable<bool> JazzCash { get; set; }
        public Nullable<bool> Contact { get; set; }
        public Nullable<bool> No_Business { get; set; }
    }
}

CustomerForm View: //只有错误部分

@Html.CheckBoxFor(Model => Model.Western_Union)
@Html.CheckBoxFor(Model => Model.Ria)//error
@Html.CheckBoxFor(Model => Model.Money_Gram)//error
@Html.CheckBoxFor(Model => Model.Intel)//error
@Html.CheckBoxFor(Model => Model.JazzCash)//error
@Html.CheckBoxFor(Model => Model.Contact)//error
@Html.CheckBoxFor(Model => Model.No_Business)//error

【问题讨论】:

  • 您可以添加视图的第一行吗?类似@model CustomerSomething

标签: c# model-view-controller checkbox boolean explicit


【解决方案1】:

你的 CustomerCount 类中有这个(=SQL 表?):

public Boolean Western_Union { get; set; }
public Boolean Ria { get; set; }
public Boolean Money_Gram { get; set; }
public Boolean Intel { get; set; }
public Boolean JazzCash { get; set; }
public Boolean Contact { get; set; }
public Boolean No_Business { get; set; }

但这在您的CustomerInfo 类中(=model?):

public Nullable<bool> Western_Union { get; set; }
public Nullable<bool> Ria { get; set; }
public Nullable<bool> Money_Gram { get; set; }
public Nullable<bool> Intel { get; set; }
public Nullable<bool> JazzCash { get; set; }
public Nullable<bool> Contact { get; set; }
public Nullable<bool> No_Business { get; set; }

所以要么删除所有Nullable,要么转换值:

// Explicit conversion as suggested by your error message:
ct.Western_Union = (bool)cc.Western_Union;
// Function getting the boolean value or false if null:
ct.Western_Union = cc.Western_Union.GetValueOrDefault();

您还可以摆脱 CustomerInfo 类并使用 CustomerCount 作为模型。


编辑: Hiba T 解决了你的问题(CheckBoxFor 有这个重载:

CheckBoxFor<TModel>(HtmlHelper<TModel>, Expression<Func<TModel,Boolean>>)

注意Boolean,而不是Nullable&lt;Boolean&gt;),但您仍然应该问自己为什么要使用两个(几乎相同的)类来处理相同的东西。

【讨论】:

  • 我正在使用实体框架从数据库中更新表
  • 那么,它有什么变化?
  • 在我的剃刀视图中,我已将 CheckBoxFor 替换为 HtmlEditorFor,现在一切正常。
【解决方案2】:

一个“?”表示可以为空的类型。布尔变量可以为真或假。可为空的布尔变量可以是真、假或空。因此,不能隐式地完成从可空 bool 到不可空 bool 的映射。

但老实说,我认为你需要阅读一些关于 c#、MVC 和 sql 的一般知识......你的方法有点令人困惑,至少对我来说是这样。

【讨论】:

    【解决方案3】:

    你的变量应该是真或假(不可为空),检查你的数据库 这个链接可能对你有帮助:CheckBoxFor

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 2011-08-23
      • 1970-01-01
      • 2019-03-06
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多