English 中文(简体)
因验证而坠毁的申请——不能将这一财产定为无效价值
原标题:Application crash due to validation - This property cannot be set to a null value

我为用户创造新项目创造了以下看法。 在用户离开现场空白时,Ive试图创建验证范围,然后生成验证信息。 然而,如果用户离开现场空白,我的申请就在以下几条线路上坠毁:_headline = structureObject.SetValidValue(价值、虚假),载于《模型》中。 这是因为:

这种财产不能归为无效。

我模式的一部分.Designer.cs文档:

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String headline
    {
        get
        {
            return _headline;
        }
        set
        {
            OnheadlineChanging(value);
            ReportPropertyChanging("headline");
      _headline = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("headline");
            OnheadlineChanged();
        }
    }

本条适用的法典部分如下:

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>

    <legend>News Details</legend>

    <br />
        Posted Date:
            <div class="editor-field">
                @Html.EditorFor(model => model.posted)
                @Html.ValidationMessageFor(model => model.posted)
            </div>

    <br />

        Headline Title:
            <div class="editor-field">
                @Html.EditorFor(model => model.headline)
                @Html.ValidationMessageFor(model => model.headline)
            </div>

    <br />

The following is my accountModels.cs file where i into the accreditation for the View:

[MetadataType(typeof(NewsValidation))]

public partial class News
{
}

public class NewsValidation
{

   [Required(ErrorMessage = "Posted date is required")]
    public DateTime posted { get; set; }

    [Required(ErrorMessage = "Headline is required")]
    [Display(Name = "Headline")]
    public string headline { get; set; }

    [Required(ErrorMessage = "Story body is required")]
   public string story { get; set; }
}

我被告知,是因为我的数据库允许Null数值,但此后,我建立了一个新的数据库,不再允许Nuslls。 我的申请仍然坠毁,也知道何时开始。 这里是一件奇怪的事情,当它坠毁时,就可点击它继续使用,而验证也出现在其中。 因此,它看上去的是验证工作,但出于某种原因,先是申请坠毁。

谁能提供任何支持?

最佳回答

问题是,贵领域模式的<代码>headline的一套财产正试图进行更新。 缺省模式在试图将行动论点与请求联系起来时,即受约束。 如果用户离开头条线,你将获得这一例外。

我非常强烈地建议你采用观点模式,从来不采纳你的领域模式。 因此界定了简单的《新闻评论》。

public class NewsViewModel
{
    public DateTime Posted { get; set; }

    [Required]
    public string Headline { get; set; }
}

并且由你的控制者采取行动,以便:

public class HeadlinesController: Controller
{
    public ActionResult Index()
    {
        var model = new NewsViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(NewsViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // there were validation errors. For example the user
            // left the headline field blank => redisplay the view
            return View(model);
        }

        // at this stage we know that validation passed => we can 
        // process our domain model.
        var news = new News();
        news.posted = model.Posted;
        news.headline = model.Headline;

        return RedirectToAction("success");
    }
}

显然,现在将强烈地把这一看法归入你们的看法模式:

@model NewsViewModel
@using (Html.BeginForm()) 
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>News Details</legend>

        <br />
        Posted Date:
        <div class="editor-field">
            @Html.EditorFor(model => model.Posted)
            @Html.ValidationMessageFor(model => model.Posted)
        </div>

        <br />

        Headline Title:
        <div class="editor-field">
            @Html.EditorFor(model => model.Headline)
            @Html.ValidationMessageFor(model => model.Headline)
        </div>

        <br />

        <button type="submit">OK</submit>
    </fieldset>
}
问题回答

如果申请坠毁,但你可以点击游戏,并乐意进行验证,那么你是否准备展示所有被抛弃的例外情况,而不仅仅是用户的无用例外? 或许StructuralObject.SetValidValue()正在使用一种例外情况进行流动控制。

  1. Go to Debug > Exceptions
  2. If you don t have a User-unhandled column, Go to Tools > Options > Debugging > General and check Enable Just My Code (Managed Only)
  3. In Debug > Exceptions, uncheck Thrown and check User-unhandled under Common Language Runtime Exceptions

Just a hunch - does it make any difference?





相关问题
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. ...

热门标签