我正在处理我的第一个MVC5应用,并在尝试预览视图时遇到以下错误:
"参数字典包含参数的空条目 ' resetType'非可空类型的System.Int32'方法 ' System.Web.Mvc.ActionResult Index(Int32)'在 ' Morpheus.Controllers.ResetPasswordController&#39 ;.可选参数 必须是引用类型,可空类型,或声明为 可选参数。参数名称:参数"
据我所知,在我看来,我需要传递一个参数。这是因为我的观点是将从另一个观点链接到的两个观点之一。最终的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; }
}
}
答案 0 :(得分:2)
问题不在你的View代码中,而是在控制器中,特别是:
public ActionResult Index(int resetType)
如果您在浏览器http://yoururl/yourcontroller/index?resetType=1
中执行此操作,则会看到更好的结果。
发生的事情是你说你的控制器必须传递一个整数(它不是一个可选参数)。 ModelBinder将尝试查看是否可以找到&#34; resetType&#34;在请求中传递的任何数据中,例如URL或任何表单数据。 ModelBinder无法找到它,因此它抱怨它无法为您的控制器提供resetType值。您可以通过在查询字符串中指定命名参数(如上所述)或使用 routes 将值放在url中的特定位置并告诉MVC选择哪个值来解决此问题。用于resetType。
要使resetType成为路线的一部分,请添加以下内容:
[Route("Login/{resetType}]
public ActionResult Index(int resetType)
然后你可以http://yoururl/yourcontroller/login/1
这告诉值提供者在url中查找该位置的整数,并将其分配给&#34; resetType&#34;。 或者,重命名&#34; resetType&#34;到&#34; id&#34;并且默认路线适合您。
public ActionResult Index(int id)
如果您希望resetType是可选的,您需要使其成为可为空的int并将路径更改为
[Route("Login/{resetType?}]
public ActionResult Index(int? resetType)
或
public ActionResult Index(int? id)
答案 1 :(得分:1)
根据您的观看表单,您的帖子操作应如下所示:
[HttpPost]
public ActionResult Index(int resetType,ResetPasswordViewModel model)
{
........
}