由于验证导致应用程序崩溃 - '此属性不能设置为空值'

时间:2012-01-13 22:08:46

标签: c# asp.net-mvc-3 validation null

我为用户创建了以下视图来创建新项目。我试图创建一些验证,因此如果用户将字段留空,则会生成验证消息。但是,如果用户确实将字段留空,则我的应用程序会在以下行崩溃:Model.Designer.cs文件中的_headline = structuralObject.SetValidValue(value, false)。因为:

  

此属性不能设置为空值。

我的Model.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 />

以下是我的AccountModels.cs文件,其中我输入了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值,但从那时起我创建了一个不再允许Null的新数据库。我的应用程序仍然崩溃,不知道从哪里开始。这是奇怪的事情,当它崩溃时,我点击播放继续,然后验证出现。因此看起来验证有效,但由于某种原因,事先应用程序崩溃。

任何人都可以提供任何支持吗?

2 个答案:

答案 0 :(得分:0)

如果应用程序崩溃但您可以单击播放并且很乐意继续并执行验证,那么您的调试是否设置为显示所有抛出的异常,而不仅仅是用户未处理的异常?也许StructuralObject.SetValidValue()正在使用流控制的例外。

  1. 转到调试&gt;例外
  2. 如果您没有“用户未处理的”字样。列,转到工具&gt;选项&gt;调试&gt;常规并选中启用仅我的代码(仅管理)
  3. 在调试&gt;例外,取消选中&#39;抛出&#39;并检查&#39;用户未处理的&#39;在公共语言运行时例外
  4. 只是预感 - 这有什么不同吗?

答案 1 :(得分:0)

问题是域模型上的headline属性的setter正在尝试执行更新。当尝试从请求绑定action参数时,默认模型绑定器将调用此setter。如果用户将标题字段留空,则会出现此异常。

我强烈建议您使用视图模型,不要将域模型传递给视图。因此,定义一个简单的NewsViewModel:

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