当我单击编辑按钮时使用mvc不显示下拉值?

时间:2017-12-08 12:59:45

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

当我点击编辑按钮时。下拉值未到,但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); }

控制器:

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

Cs.HTML:

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

    }

型号:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>io.pivotal.microservices.services.Main</start-class>
        <spring-cloud.version>Brixton.RELEASE</spring-cloud.version>
    </properties>

1 个答案:

答案 0 :(得分:1)

&lt; p&gt;问题是您已定义&lt; code&gt; DomainID&lt; / code&gt;为:或其; / p为H. &lt; pre&gt;&lt; code&gt; public int DomainID {get;组; } &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;在C#中,但它是&lt; code&gt; bigint&lt; / code&gt; (即&lt; code&gt; long&lt; / code&gt;)在数据库中。因此,您需要使用:&lt; / p&gt; &lt; pre&gt;&lt; code&gt; public long DomainID {get;组; } &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;并更改所有其他&lt; code&gt; int&lt; / code&gt; &LT;代码&GT; DOMAINID&LT; /代码&GT;参考也是如此。例如:&lt; / p&gt; &lt; pre&gt;&lt; code&gt; long domainID = ModID.DomainID; &LT; /代码&GT;&LT; /预&GT;