无法将类型为“System.Int32”的对象强制转换为类型“System.Array”

时间:2020-11-24 13:32:29
修改Edit.cshtml上部分字段,点击保存跳转到Index.cshtml。
但现在点击保存,就跳出以下错误。

无法将类型为“System.Int32”的对象强制转换为类型“System.Array”

**这是Model **

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace MvcSilverTalent.Models
{
    public class RetireeModel
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int RetireNum { get; set; }

        [Required]
        [DisplayName("单位编号")]
        [MaxLength(3)]
        public string UnitID { get; set; }

        [Required]
        [DisplayName("员工编号")]
        [MaxLength(6)]
        public string EmployeeID { get; set; }

        [Required]
        [DisplayName("离退休类型")]
        public int RetireType { get; set; }

        [Required]
        [DisplayName("删除标记")]
        public int DeleteFlag { get; set; }


        //
        // 基本信息


        [Required]
        [DisplayName("姓名")]
        [MaxLength(20)]
        public string Name { get; set; }

        [Required]
        [DisplayName("性别")]
        public Boolean Gender { get; set; }

        [Required]
        [DisplayName("身份证号码")]
        public string IDCardNo { get; set; }

        [Required]
        [DisplayName("出生年月")]
        public DateTime Brityday { get; set; }

        [Required]
        [DisplayName("民族")]
        [MaxLength(30)]
        public string Nationality { get; set; }

        [Required]
        [DisplayName("籍贯")]
        [MaxLength(30)]
        public string NativePlace { get; set; }

        [DisplayName("党派")]
        [MaxLength(30)]
        public string Party { get; set; }

        [DisplayName("所在党支部")]
        [MaxLength(100)]
        public string PartyBranch { get; set; }

        [DisplayName("最高学历")]
        [MaxLength(20)]
        public string EducationDegree { get; set; }

        [Required]
        [DisplayName("用户组")]
        public string GroupName { get; set; }

        [Required]
        [DisplayName("是否参加爱心基金")]
        public Boolean HasLoveFund { get; set; }

        [DisplayName("享受爱心基金慰问情况")]
        public string LoveFundCase { get; set; }

        [DisplayName("慰问走访情况")]
        public string VisitCase { get; set; }


        //
        // 工作信息


        [Required]
        [DisplayName("参加工作时期")]
        public int WorkPeriod { get; set; }

        [Required]
        [DisplayName("参加工作时间")]
        public DateTime WorkTime { get; set; }

        [Required]
        [DisplayName("退休时间")]
        public DateTime RetireTime { get; set; }

        [Required]
        [DisplayName("工龄")]
        public int WorkAge { get; set; }

        [Required]
        [DisplayName("退休单位")]
        [MaxLength(100)]
        public string UnitName { get; set; }

        [DisplayName("职称代码")]
        [MaxLength(3)]
        public int TitleID { get; set; }

        [DisplayName("职称")]
        [MaxLength(30)]
        public string Title { get; set; }

        [DisplayName("职务")]
        [MaxLength(30)]
        public string Position { get; set; }

        [DisplayName("编制")]
        [MaxLength(30)]
        public string Formation { get; set; }


        //
        // 可修改信息 


        [Required]
        [MaxLength(255)]
        [DisplayName("近照")]
        public string PhotoUrl { get; set; }

        [MaxLength(255)]
        [DisplayName("特长")]
        public string Specialty { get; set; }

        [Required]
        [MaxLength(255)]
        [DisplayName("现住地址")]
        public string Address { get; set; }

        [MaxLength(30)]
        [DisplayName("家庭电话")]
        public string HomeTel { get; set; }

        [MaxLength(30)]
        [DisplayName("手机号码")]
        public string MobileNum { get; set; }

        [MaxLength(10)]
        [DisplayName("邮政编码")]
        public string ZipCode { get; set; }

        [MaxLength(40)]
        [DisplayName("邮箱")]
        public string Email { get; set; }


        public virtual ICollection<EventSignModel> EventSignModels { get; set; }
        public virtual ICollection<HealthSignMain> HealthSignMains { get; set; }
    }
}


**这是controller **


      
  // GET: /UserInfo/Edit.cshtml

        public ActionResult Edit(int RetireNum) 
        {
            RetireeModel RetireeModel = db.RetireeModels.Find(RetireNum);
            if (RetireeModel == null)
            {
                return HttpNotFound();
            }
            return View(RetireeModel);
        }


        //
        // POST: /UserInfo/Edit.cshtml

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(RetireeModel RetireeModel)
        {
            Response.Write("<script>alert('ddddd')</script>");

            if (ModelState.IsValid)
            {
                db.Entry(RetireeModel).State = EntityState.Modified;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            Response.Write("<script>alert('xxxx')</script>");

            return View("Index");
        }





  

9 个解决方案

#1




这是修改提交的表单 view

 @model MvcSilverTalent.Models.RetireeModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    @using (Html.BeginForm("Edit","Userinfo")) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>RetireeModel</legend>

            @Html.HiddenFor(model => model.RetireNum)

            <div class="editor-label">
                @Html.LabelFor(model => model.Specialty)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Specialty)
                @Html.ValidationMessageFor(model => model.Specialty)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Address)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Address)
                @Html.ValidationMessageFor(model => model.Address)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.HomeTel)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.HomeTel)
                @Html.ValidationMessageFor(model => model.HomeTel)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.MobileNum)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.MobileNum)
                @Html.ValidationMessageFor(model => model.MobileNum)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.ZipCode)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.ZipCode)
                @Html.ValidationMessageFor(model => model.ZipCode)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>

            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>

#2


字段对应不上了,改错了被

#3


错误信息 不足   把你报错的信息拿出来看看吧

#4


将你那两个ICollection注释掉看看是不是页面上有同名的

#5


引用 4 楼 starfd 的回复:
将你那两个ICollection注释掉看看是不是页面上有同名的

没看懂。。这么多东西。。
楼主撤销修改。。。然后改一个字段保存下试试得了。。。。。 无法将类型为“System.Int32”的对象强制转换为类型“System.Array”

#6


我也是刚用MVC,不是很熟悉,但看代码没看出问题所在

其实楼上你这个问题容易解决:【断点】跟踪,把断点断在更新的那方法上,F10一步步调,看问题报在哪个块就懂了,加上是类型转换异常,很容易就查出来的

最后问一下楼主,我看你控制器里使用Response.Write("<script>alert('ddddd')</script>");,我想请教一下,这种有效果不?我昨晚还折腾这个,MVC在后台调用前台JS真是让人蛋疼,我还不理解机制,昨晚想到快吐血去

#7


引用 6 楼 likevs 的回复:
我也是刚用MVC,不是很熟悉,但看代码没看出问题所在

其实楼上你这个问题容易解决:【断点】跟踪,把断点断在更新的那方法上,F10一步步调,看问题报在哪个块就懂了,加上是类型转换异常,很容易就查出来的

最后问一下楼主,我看你控制器里使用Response.Write("<script>alert('ddddd')</script>");,我想请教一下,这种有效果不?我昨晚还折腾这个,MVC在后台调用前台JS真是让人蛋疼,我还不理解机制,昨晚想到快吐血去

这跟MVC不MVC的无关
后台根本不能调用前台JS,只能拼接前台JS,输出到html,然后让IE去执行
所以你想在后台弹出个确认,前台点完继续执行后台的方法,那是不可行的.因为等前台看到弹出窗,后台代码早执行完了

#8


引用 7 楼 Z65443344 的回复:
Quote: 引用 6 楼 likevs 的回复:

我也是刚用MVC,不是很熟悉,但看代码没看出问题所在

其实楼上你这个问题容易解决:【断点】跟踪,把断点断在更新的那方法上,F10一步步调,看问题报在哪个块就懂了,加上是类型转换异常,很容易就查出来的

最后问一下楼主,我看你控制器里使用Response.Write("<script>alert('ddddd')</script>");,我想请教一下,这种有效果不?我昨晚还折腾这个,MVC在后台调用前台JS真是让人蛋疼,我还不理解机制,昨晚想到快吐血去

这跟MVC不MVC的无关
后台根本不能调用前台JS,只能拼接前台JS,输出到html,然后让IE去执行
所以你想在后台弹出个确认,前台点完继续执行后台的方法,那是不可行的.因为等前台看到弹出窗,后台代码早执行完了

我知道,但是我玩WebForm的,感觉微软帮封装得很多工作,开发起来效率高很多,比如
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MyAlert", "alert('" + msg + "');", true);

我可以在按钮提交事件里输出JS代码,但MVC模式的,我要在保存成功之后,提示这个JS,就要做好多工作,反正我觉得效率好低

#9


[DisplayName("职称代码")]
        [MaxLength(3)]
        public int TitleID { get; set; }

int 不能用
MaxLength

#1




这是修改提交的表单 view

 @model MvcSilverTalent.Models.RetireeModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    @using (Html.BeginForm("Edit","Userinfo")) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>RetireeModel</legend>

            @Html.HiddenFor(model => model.RetireNum)

            <div class="editor-label">
                @Html.LabelFor(model => model.Specialty)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Specialty)
                @Html.ValidationMessageFor(model => model.Specialty)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Address)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Address)
                @Html.ValidationMessageFor(model => model.Address)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.HomeTel)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.HomeTel)
                @Html.ValidationMessageFor(model => model.HomeTel)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.MobileNum)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.MobileNum)
                @Html.ValidationMessageFor(model => model.MobileNum)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.ZipCode)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.ZipCode)
                @Html.ValidationMessageFor(model => model.ZipCode)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>

            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>

#2


字段对应不上了,改错了被

#3


错误信息 不足   把你报错的信息拿出来看看吧

#4


将你那两个ICollection注释掉看看是不是页面上有同名的

#5


引用 4 楼 starfd 的回复:
将你那两个ICollection注释掉看看是不是页面上有同名的

没看懂。。这么多东西。。
楼主撤销修改。。。然后改一个字段保存下试试得了。。。。。 无法将类型为“System.Int32”的对象强制转换为类型“System.Array”

#6


我也是刚用MVC,不是很熟悉,但看代码没看出问题所在

其实楼上你这个问题容易解决:【断点】跟踪,把断点断在更新的那方法上,F10一步步调,看问题报在哪个块就懂了,加上是类型转换异常,很容易就查出来的

最后问一下楼主,我看你控制器里使用Response.Write("<script>alert('ddddd')</script>");,我想请教一下,这种有效果不?我昨晚还折腾这个,MVC在后台调用前台JS真是让人蛋疼,我还不理解机制,昨晚想到快吐血去

#7


引用 6 楼 likevs 的回复:
我也是刚用MVC,不是很熟悉,但看代码没看出问题所在

其实楼上你这个问题容易解决:【断点】跟踪,把断点断在更新的那方法上,F10一步步调,看问题报在哪个块就懂了,加上是类型转换异常,很容易就查出来的

最后问一下楼主,我看你控制器里使用Response.Write("<script>alert('ddddd')</script>");,我想请教一下,这种有效果不?我昨晚还折腾这个,MVC在后台调用前台JS真是让人蛋疼,我还不理解机制,昨晚想到快吐血去

这跟MVC不MVC的无关
后台根本不能调用前台JS,只能拼接前台JS,输出到html,然后让IE去执行
所以你想在后台弹出个确认,前台点完继续执行后台的方法,那是不可行的.因为等前台看到弹出窗,后台代码早执行完了

#8


引用 7 楼 Z65443344 的回复:
Quote: 引用 6 楼 likevs 的回复:

我也是刚用MVC,不是很熟悉,但看代码没看出问题所在

其实楼上你这个问题容易解决:【断点】跟踪,把断点断在更新的那方法上,F10一步步调,看问题报在哪个块就懂了,加上是类型转换异常,很容易就查出来的

最后问一下楼主,我看你控制器里使用Response.Write("<script>alert('ddddd')</script>");,我想请教一下,这种有效果不?我昨晚还折腾这个,MVC在后台调用前台JS真是让人蛋疼,我还不理解机制,昨晚想到快吐血去

这跟MVC不MVC的无关
后台根本不能调用前台JS,只能拼接前台JS,输出到html,然后让IE去执行
所以你想在后台弹出个确认,前台点完继续执行后台的方法,那是不可行的.因为等前台看到弹出窗,后台代码早执行完了

我知道,但是我玩WebForm的,感觉微软帮封装得很多工作,开发起来效率高很多,比如
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MyAlert", "alert('" + msg + "');", true);

我可以在按钮提交事件里输出JS代码,但MVC模式的,我要在保存成功之后,提示这个JS,就要做好多工作,反正我觉得效率好低

#9


[DisplayName("职称代码")]
        [MaxLength(3)]
        public int TitleID { get; set; }

int 不能用
MaxLength