-1

When i click edit button. the drop down value is not coming but in ViewBag.DomainID value is present but it is not displaying. it shows The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not valid.

Controller:

[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>

Models:

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

    }
Ivin Raj
  • 3,448
  • 2
  • 28
  • 65

1 Answers1

1

The problem is that you have defined DomainID as:

public int DomainID { get; set; }

in C#, but it is a bigint (i.e. long) in the database. Thus instead you need to use:

public long DomainID { get; set; } 

And change all other int DomainID references as well. Such as:

long domainID = ModID.DomainID;
mjwills
  • 23,389
  • 6
  • 40
  • 63