Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception ASP.NET MVC3:TryUpdateModel引发异常_Exception_Asp.net Mvc 3_Model_Updatemodel - Fatal编程技术网

Exception ASP.NET MVC3:TryUpdateModel引发异常

Exception ASP.NET MVC3:TryUpdateModel引发异常,exception,asp.net-mvc-3,model,updatemodel,Exception,Asp.net Mvc 3,Model,Updatemodel,我有一个接受以下模型的视图: Inherits="System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel>" 但是,回发时会引发以下异常: 无法将“System.Int32”类型的对象强制转换为“System.String”类型 描述:执行当前web请求期间发生未处理的异常。 请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.InvalidCastException:无法将类

我有一个接受以下模型的视图:

Inherits="System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel>"
但是,回发时会引发以下异常:

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

描述:执行当前web请求期间发生未处理的异常。 请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.InvalidCastException:无法将类型为“System.Int32”的对象强制转换为类型为“System.String”

源错误:

虽然我不能在这里突出显示它,但异常发生在第137行。我无法单步执行此方法,因此如何确定导致此异常的原因

另外,TryUpdateModel不应该让我免于任何异常,只是返回true或false来反映结果吗?我不知道它会抛出异常

更新:以下是模型(包括派生模型和基础模型):

最后,这里是视图的相关部分:

    <% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Job Details</legend>

        <div class="editor-label">
            Job description
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobDescription)%>
            <%: Html.ValidationMessageFor(model => model.JobDescription)%>
        </div>

        <div class="editor-label">
            PO number
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.PONumber)%>
            <%: Html.ValidationMessageFor(model => model.PONumber)%>
        </div>

        <div class="editor-label">
            Date received
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.ReceivedDate)%>
            <%: Html.ValidationMessageFor(model => model.ReceivedDate)%>
        </div>

        <div class="editor-label">
            Deadline Date
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.DeadlineDate)%>
            <%: Html.ValidationMessageFor(model => model.DeadlineDate)%>
        </div>

        <div class="editor-label">
            Date sent
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.SentDate)%>
            <%: Html.ValidationMessageFor(model => model.SentDate)%>
        </div>

        <div class="editor-label">
            Is fixed-cost?&nbsp;<input type="checkbox" id="fixed-cost" />
        </div>

        <div id="word-priced-job">
            <div class="editor-label">
                Word count
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.WordCount)%>
                <%: Html.ValidationMessageFor(model => model.WordCount)%>
            </div>

            <div class="editor-label">
                Word cost
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.WordCost)%>
                <%: Html.ValidationMessageFor(model => model.WordCost)%>
            </div>
        </div>

        <div id="fixed-price-job" class="faded">
            <div class="editor-label">
                Fixed cost
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.FixedCost)%>
                <%: Html.ValidationMessageFor(model => model.FixedCost)%>
            </div>
        </div>

        <div class="editor-label">
            Languages
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.LanguageFrom, Model.Languages, "-- Select --") %><%: Html.ValidationMessageFor(model => model.LanguageFrom)%>
            <span>&nbsp;-&nbsp;to&nbsp;-&nbsp;</span>
            <%: Html.DropDownListFor(model => model.LanguageTo, Model.Languages, "-- Select --") %><%: Html.ValidationMessageFor(model => model.LanguageTo)%>
        </div>

        <div class="editor-label">
            Client
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.Client, Model.ClientsList, "-- Select --") %> <%: Html.ActionLink("Create a new client", "Create", "Clients") %>
            <%: Html.ValidationMessageFor(model => model.Client)%>
        </div>

        <div class="editor-label">
            Job file name
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobFileName) %>
            <%: Html.ValidationMessageFor(model => model.JobFileName)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.WorkedBy)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.WorkedBy, Model.Users) %>
        </div>

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

<% } %>

工作细节
职位描述
型号.职务说明)%>
型号.职务说明)%>
采购订单号
型号(编号)%%>
型号(编号)%%>
收到日期
型号.接收日期)%>
型号.接收日期)%>
截止日期
型号.截止日期)%>
型号.截止日期)%>
发送日期
型号(日期)%>
型号(日期)%>
是固定成本吗?
字数
model.WordCount)%%>
model.WordCount)%%>
文字成本
model.WordCost)%>
model.WordCost)%>
固定成本
型号.固定成本)%>
型号.固定成本)%>
语言文字
model.LanguageFrom,model.Languages,“--Select--”%>model.LanguageFrom)%%>
-至-
model.LanguageTo,model.LanguageTo,“--Select--”%>model.LanguageTo)%%>
客户
model.Client,model.ClientsList,“--Select-->”
model.Client)%%>
作业文件名
model.JobFileName)%%>
model.JobFileName)%%>
型号.工作人员)%>
model.WorkedBy,model.Users)%>


不知道为什么在没有看到模型和视图代码的情况下会发生此异常,但请尝试简化您的操作,如下所示:

[HttpPost]
public ActionResult Create(JobCreationModel job)
{
    if (!ModelState.IsValid)
    {
        return View(job);
    }
    _jobRepository.AddJob(job);
    _jobRepository.Save();
    return RedirectToAction("Index");
}

在没有看到模型和视图代码的情况下,不知道为什么会发生此异常,但请尝试简化您的操作,如下所示:

[HttpPost]
public ActionResult Create(JobCreationModel job)
{
    if (!ModelState.IsValid)
    {
        return View(job);
    }
    _jobRepository.AddJob(job);
    _jobRepository.Save();
    return RedirectToAction("Index");
}
我改变了:

[StringLength(3)]
public int? GroupID { get; set; }
致:

所以StringLength属性出现了问题,因为GroupID是int?StringLength属性似乎只对字符串有效。

我已更改:

[StringLength(3)]
public int? GroupID { get; set; }
致:


所以StringLength属性出现了问题,因为GroupID是int?StringLength属性似乎只对字符串有效。

现在唯一的区别是,放置在此操作中任何位置的断点不再被命中,但异常消息仍会出现。@awj,如果您在该
JobCreationModel
类的构造函数中放置断点,是否调用它?这门课看起来真是一团糟。我建议您使用POCO视图模型,然后将它们映射回您的模型。是做这项工作的好工具。您仍然可以保留这个混乱的模型,但是让控制器操作从视图中获取一个干净的视图模型。通过删除代码的不同部分来缩小问题的范围,直到找到问题所在。@DD-谢谢你的建议。这有点超出了我原来问题的范围,所以我可能会也可能不会在这里发帖,但谢谢你的建议-我会继续跟进。现在唯一的区别是,在这个操作中放置的断点不再被击中,但异常的消息仍然会出现。@awj,如果在这个
JobCreationModel
类的构造函数中放置断点,是否调用它?这门课看起来真是一团糟。我建议您使用POCO视图模型,然后将它们映射回您的模型。是做这项工作的好工具。您仍然可以保留这个混乱的模型,但是让控制器操作从视图中获取一个干净的视图模型。通过删除代码的不同部分来缩小问题的范围,直到找到问题所在。@DD-谢谢你的建议。这有点超出了我最初问题的范围,所以我可能会也可能不会在这里发帖,但谢谢你的建议-我会跟进。我遇到了同样的问题,我使用的是普通类-没有entityframework或类似的东西。我遇到了同样的问题,我使用的是普通类-没有entityframework或类似的东西。
    <% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Job Details</legend>

        <div class="editor-label">
            Job description
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobDescription)%>
            <%: Html.ValidationMessageFor(model => model.JobDescription)%>
        </div>

        <div class="editor-label">
            PO number
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.PONumber)%>
            <%: Html.ValidationMessageFor(model => model.PONumber)%>
        </div>

        <div class="editor-label">
            Date received
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.ReceivedDate)%>
            <%: Html.ValidationMessageFor(model => model.ReceivedDate)%>
        </div>

        <div class="editor-label">
            Deadline Date
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.DeadlineDate)%>
            <%: Html.ValidationMessageFor(model => model.DeadlineDate)%>
        </div>

        <div class="editor-label">
            Date sent
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.SentDate)%>
            <%: Html.ValidationMessageFor(model => model.SentDate)%>
        </div>

        <div class="editor-label">
            Is fixed-cost?&nbsp;<input type="checkbox" id="fixed-cost" />
        </div>

        <div id="word-priced-job">
            <div class="editor-label">
                Word count
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.WordCount)%>
                <%: Html.ValidationMessageFor(model => model.WordCount)%>
            </div>

            <div class="editor-label">
                Word cost
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.WordCost)%>
                <%: Html.ValidationMessageFor(model => model.WordCost)%>
            </div>
        </div>

        <div id="fixed-price-job" class="faded">
            <div class="editor-label">
                Fixed cost
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.FixedCost)%>
                <%: Html.ValidationMessageFor(model => model.FixedCost)%>
            </div>
        </div>

        <div class="editor-label">
            Languages
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.LanguageFrom, Model.Languages, "-- Select --") %><%: Html.ValidationMessageFor(model => model.LanguageFrom)%>
            <span>&nbsp;-&nbsp;to&nbsp;-&nbsp;</span>
            <%: Html.DropDownListFor(model => model.LanguageTo, Model.Languages, "-- Select --") %><%: Html.ValidationMessageFor(model => model.LanguageTo)%>
        </div>

        <div class="editor-label">
            Client
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.Client, Model.ClientsList, "-- Select --") %> <%: Html.ActionLink("Create a new client", "Create", "Clients") %>
            <%: Html.ValidationMessageFor(model => model.Client)%>
        </div>

        <div class="editor-label">
            Job file name
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobFileName) %>
            <%: Html.ValidationMessageFor(model => model.JobFileName)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.WorkedBy)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.WorkedBy, Model.Users) %>
        </div>

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

<% } %>
[HttpPost]
public ActionResult Create(JobCreationModel job)
{
    if (!ModelState.IsValid)
    {
        return View(job);
    }
    _jobRepository.AddJob(job);
    _jobRepository.Save();
    return RedirectToAction("Index");
}
[StringLength(3)]
public int? GroupID { get; set; }
public int? GroupID { get; set; }