English 中文(简体)
MVC 错误: 输入字典的模型项目无效。
原标题:MVC Error: The model item passed into the dictionary is null

我只是试图形成一种看法,但我错了以下错误:

System.InvalidOperationException: The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type System.DateTime

现在,我知道为什么出现这种情况,但数据库is上的特殊领域却被宣布为无效,因为这是以后编辑的。 我的守则是:

Action

public ActionResult View(Int64? Id)
    {

        ModelContainer ctn = new ModelContainer();
        var item = from t in ctn.Items where t.ItemID == Id select t;
        return View(Item.First());
    }

http://www.ohchr.org。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Administrator.Master" Inherits="System.Web.Mvc.ViewPage<myApp.Data.Item>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    View
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <% Html.RenderPartial("Details", Model); %>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
<h1>Details - <%= Model.MainItem %></h1>
</asp:Content>

<>部分观点>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<myApp.Data.Item>" %>
<%@ Import Namespace="myApp.Supplier.Web.Extensions" %>

    <fieldset>

        <legend>Information</legend>

        <div class="fieldset">

            <%= Html.LabelFor(m => m.MainItem)%>
            <%= Html.DisplayFor(m => m.MainItem, "FormTextShort")%><br />
            <%= Html.LabelFor(m => m.Supplier.Name)%>
            <%= Html.DisplayFor(m => m.Supplier.Name, "FormTextShort")%><br />
            <%= Html.LabelFor(m => m.ProductCode)%>
            <%= Html.DisplayFor(m => m.ProductCode, "FormTextShort")%><br />
            <%= Html.LabelFor(m => m.Product.SubmissionDate)%>
            <%= Html.DisplayFor(m => m.Product.SubmissionDate, "FormDateShort")%><br />
            <%= Html.LabelFor(m => m.Product.SentForRepair)%>
            <%= Html.DisplayFor(m => m.Product.SentForRepair, "FormDateShort")%><br />
         </div>

    </fieldset>

在本案中,X。 产品。 由于在提交申请时,该申请尚未被撤销,因此中止原封不动。 我还有其他领域,如总数。 然而,就简便而言,我没有把这些费用列入这里。 如果我评论了SentForRepair的话,该观点与其他信息完全吻合。

如果有人能向我说明如何克服这一错误,我会非常感激!

最佳回答

在显示模板中,你需要检查无效(在严格分类为<代码>Datetime?之后):

<% if (Model.HasValue) { %>
    <%= Html.Encode(Model.Value.ToString("yyyy-MM-dd")) %>
<% } %>

或者如果你想要简单地提供习惯日期格式,你可以删除<代码>。 表格DateShort 显示模版,把你的眼光模型财产与<代码>脱钩。

[DisplayFormat(NullDisplayText = "", DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? SentForRepair { get; set; }

and then simply:

<%= Html.DisplayFor(m => m.Product.SentForRepair)%>
问题回答

我在此找到了另一种解决办法。 设定价值为无效。

Telerik MVC Grid - problem with disable Time property

@model System.DateTime?

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, 
    new { data_datepicker = true });

更改您的定制编辑模板,从类型日期改为无效日期(时间?)

@model System.DateTime?

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
                      new { @class = "text-box single-line", data_datepicker="true"})

您 表格 短模板在某一时间使用? 不是时间,而是用某种逻辑检查你在提货之前具有价值。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签