【问题标题】:ASP.NET MVC DropDownFor Validation (Value cannot be null. Parameter name: source)ASP.NET MVC DropDownFor 验证(值不能为空。参数名称:源)
【发布时间】:2011-02-28 22:01:33
【问题描述】:

我仍在努力学习 ASP.NET MVC。我所有的表单条目都是必需的,所以我想对它们进行验证。为简洁起见,我将模型与描述(文本框)和范式(下拉菜单)配对。我包括 Entry.cs、Paradigm.cs 和 EntryViewModel.cs 模型类和 Display.cshtml 视图。

[Bind(Exclude = "EntryId")]
public class Entry
{
  [ScaffoldColumn(false)] 
  public int EntryId { get; set; }

  [Required(ErrorMessage = "You must include a description.")]
  public string Description { get; set; }

  [Display(Name = "Type")]
  [Required(ErrorMessage = "You must select a type.")]
  public int ParadigmId { get; set; }

  public virtual Paradigm Paradigm { get; set; }
}

public class Paradigm
{
    [ScaffoldColumn(false)]
    public int ParadigmId { get; set; }

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

    public List<Entry> Entries { get; set; } 
}

public class EntryViewModel
{
    public Entry Entry { get; set; }
    public IEnumerable<Entry> Entries { get; set; }
}

@model Pylon.Models.EntryViewModel

@{
    ViewBag.Title = "Display";
}

<hr />

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Entry</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Entry.Description)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Entry.Description)
            @Html.ValidationMessageFor(model => model.Entry.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Entry.ParadigmId)
        </div>
        <div class="editor-field">   
            @Html.DropDownListFor(model => model.Entry.ParadigmId, ((IEnumerable<Pylon.Models.Paradigm>)ViewBag.PossibleParadigms).Select(option => new SelectListItem {
                Text = (option == null ? "None" : option.Name), 
                Value = option.ParadigmId.ToString(),
                Selected = (Model != null) && (option.ParadigmId == Model.Entry.ParadigmId)
            }))
            <a href="@Url.Action("Paradigm")"><img src="../../Content/Images/add_icon.gif" /></a> 
            @Html.ValidationMessageFor(model => model.Entry.ParadigmId)
        </div>

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

如果我提交表单时未输入说明,我希望验证启动并说“您必须包含说明”。但是,我在 DropDownFor 行上收到了 ArgumentNullException。 http://www.wvha.org/temp/ArgumentNullException.png

我该怎么办?顺便说一句,涵盖 ASP.NET MVC 3/Razor 的任何体面的书籍。我可以遵循基本的 tuts,但是当我需要转向更高级的功能时,我会误入歧途。

public class EntriesController : Controller
    {
        private readonly PylonContext _context = new PylonContext();

        public ActionResult Display()
        {
            // DropDown
            ViewBag.PossibleParadigms = _context.Paradigms;

            var viewModel = new EntryViewModel {Entries = _context.Entries.ToList()};
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Display(EntryViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                _context.Entries.Add(viewModel.Entry);
                _context.SaveChanges();
                return RedirectToAction("Display");
            }

            return View(viewModel);
        }
}

【问题讨论】:

  • 向我们展示您对 DropDownFor 的调用。很有可能,它无法弄清楚应该用什么来填充下拉列表。
  • 提示:与其在 Visual Studio 中发布异常的屏幕截图,不如发布堆栈跟踪信息更有用,它可以准确显示异常的来源。您可以从异常消息中的 View detail... 链接获取堆栈跟踪。

标签: asp.net-mvc razor


【解决方案1】:

如果没有看到您的控制器代码,这很难说,但看起来您的 ViewBag.PossibleParadigms 可能为空。

您的插入/更新控制器操作看起来像这样吗?

if (ModelState.IsValid) {
    ///...
} else {
    return View(model);
}

如果是这样,您需要在返回视图之前将 PossibleParadigms 放回 ViewBag(可以这么说)。

如果能贴出相关的控制器动作代码,肯定更容易知道。

【讨论】:

  • 超级棒我真的很喜欢 StackOverflow 用户的快速响应和准确修复。谢谢你。我将以下代码添加到控制器中的 [HTTPPost] 操作中。 ViewBag.PossibleParadigms = _context.Paradigms; viewModel.Entries = _context.Entries.ToList();
  • 不客气。投票选出有用的答案是表达感谢的好方法:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多