【问题标题】:When i click edit button Dropdown value is not display using mvc? [duplicate]当我单击编辑按钮时,下拉值不使用 mvc 显示? [复制]
【发布时间】:2018-05-22 17:36:21
【问题描述】:

当我点击编辑按钮时。下拉值未出现,但ViewBag.DomainID 值存在但未显示。它显示The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not valid.

控制器:

[Authorize]
        [MyExceptionHandler]
        public ActionResult EditModules(int id)
        {
            EditDomain_Bind();
            userType type = new userType();
            var ModID = type.GetEditRoleModulesViews(id).Find(Emp => Emp.ModuleID == id);
            int domainID = ModID.DomainID;
            AccountContext db = new AccountContext();
            string selected = (from sub in db.domain
                               where sub.DomainID == domainID
                               select sub.DomainName).First();
            ViewBag.DomainID = new SelectList(db.domain, "DomainID", "DomainName", selected);

            return View(ModID);
        }

Cs.HTML:

<div class="col-lg-4">
                            <fieldset class="form-group">
                                <label class="form-label" for="exampleInput">Domain Name</label>
                                @Html.DropDownList("DomainID")
                                @Html.ValidationMessageFor(model => Model.DomainID, null, new { @style = "color: red" })
                            </fieldset>
                        </div>

型号:

public DbSet<Domain> domain { get; set; }
    public class Domain
    {
        [Key]
        public int DomainID { get; set; }      
        public string DomainName { get; set; }

    }

【问题讨论】:

  • 你db的数据类型和你的模型一样吗?
  • @PranayRana 检查我的照片
  • @mjwills ibb.co/kpp35G 查一下
  • 值存在于字符串 Selected @mjwills
  • ibb.co/fSifkG@mjwills

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3


【解决方案1】:

问题是您已将DomainID 定义为:

public int DomainID { get; set; }

在 C# 中,但它是数据库中的 bigint(即 long)。因此,您需要使用:

public long DomainID { get; set; } 

并更改所有其他 int DomainID 引用。如:

long domainID = ModID.DomainID;

【讨论】:

  • 工作先生,谢谢
  • 我需要一个帮助下拉列表仅获取 ID 值..我想在下拉列表中获取域名
  • 我建议为 @IvinRaj 创建一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多