【发布时间】:2014-09-10 10:27:08
【问题描述】:
我正在开发我的第一个 MVC5 应用程序,在尝试预览视图时遇到以下错误:
"参数字典包含参数的空条目 方法的不可为空类型“System.Int32”的“resetType” 'System.Web.Mvc.ActionResult 索引(Int32)' 在 'Morpheus.Controllers.ResetPasswordController'。可选参数 必须是引用类型、可空类型或声明为 可选参数。参数名称:parameters"
我知道在我看来我需要传递一个参数。这是因为我的视图是从另一个视图链接到的两个视图之一。最终 URL 将是:/ResetPassword?resetType=1。我不知道如何将 resetType = 1 传递给控制器方法。我在 Razor 表单标签中尝试了它,并根据这篇文章的隐藏输入:The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'
任何帮助将不胜感激。非常感谢。如果我可以发布更多代码,请告诉我。
查看代码:
@model Morpheus.Models.ResetPasswordViewModel
@{
ViewBag.Title = "Reset Password";
}
<div class="main-content mbxl">
<div class="content-frame-large-header mbl">
<h1>Enter Your Name and Email Address</h1>
@using (Html.BeginForm("Index", "ResetPassword", new {resetType = 1}, FormMethod.Post))
{
@Html.HtmlSafeValidationSummary("An Error Occurred")
<fieldset class="mbxl">
<h3>Name</h3>
<ul class="form-list">
<li class="labelset">
<span class="label-container">
<span class="label">
@Html.LabelFor(a => a.GivenName, "Given Name")
<span class="form-note">First Name</span>
</span>
</span>
<span class="field-container">
<span class="field">
@Html.TextBoxFor(a => a.GivenName, new { placeholder = "Given Name (First Name)", maxlength = "50", id = "given-name", name = "given-name", required = "required", aria_required = "true" })
</span>
</span>
</li>
<li class="labelset">
<span class="label-container">
<span class="label">
@Html.LabelFor(a => a.FamilyName, "Family Name")
<span class="form-note">Last Name</span>
</span>
</span>
<span class="field-container">
<span class="field">
@Html.TextBoxFor(a => a.FamilyName, new { placeholder = "Family Name (Last Name)", maxlength = "50", id = "last-name", name = "last-name", required = "required", aria_required = "true" })
</span>
</span>
</li>
</ul>
</fieldset>
<fieldset class="mbxl">
<h3>Email</h3>
<ul class="form-list">
<li class="labelset">
<span class="label-container">
<span class="label">
@Html.LabelFor(a => a.Email, "Email Address")
</span>
</span>
<span class="field-container">
<span class="field">
@Html.TextBoxFor(a => a.Email, new { placeholder = "Email Address", maxlength = "50", id = "email", name = "email", required = "required", aria_required = "true" })
</span>
</span>
</li>
</ul>
</fieldset>
<div class="form-submit">
<input type="hidden" name="HomePageUrl" value="@ViewData["HomePageUrl"]" />
<input type="hidden" name="ResetType" value="@ViewData["ResetType"]" />
<input type="submit" value="Submit" class="button" />
</div>
}
</div>
</div>
控制器:
public ActionResult Index(int resetType)
{
return View(new ResetPasswordViewModel() { ResetType = resetType });
}
添加型号代码:
using System.ComponentModel.DataAnnotations;
namespace Morpheus.Models
{
public class ResetPasswordViewModel
{
public ResetPasswordViewModel()
{
}
public int ResetType { get; set; }
[Required(ErrorMessageResourceType = typeof(ControllerResources), ErrorMessageResourceName = "GivenName_Required")]
public string GivenName { get; set; }
[Required(ErrorMessageResourceType = typeof(ControllerResources), ErrorMessageResourceName = "FamilyName_Required")]
public string FamilyName { get; set; }
[Required(ErrorMessageResourceType = typeof(ControllerResources), ErrorMessageResourceName = "Email_Required")]
public string Email { get; set; }
}
}
【问题讨论】:
-
请提供
ResetPasswordViewModel的代码 -
谢谢!我已经在上面添加了。
标签: c# asp.net-mvc razor asp.net-mvc-5