【问题标题】:View Error The parameters dictionary contains a null entry for parameter查看错误参数字典包含参数的空条目
【发布时间】: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


【解决方案1】:

问题不在于您的 View 代码,而在于控制器,特别是:

public ActionResult Index(int resetType)

如果您在浏览器http://yoururl/yourcontroller/index?resetType=1 中执行此操作,您应该会看到更好的结果。

发生的事情是你说你的控制器必须被传递一个整数(它不是一个可选参数)。 ModelBinder 将尝试查看它是否可以在请求中传递的任何数据中找到“resetType”,例如在 URL 或任何表单数据中。 ModelBinder 找不到,因此它抱怨它无法为您的控制器提供 resetType 的值。您可以通过在查询字符串中指定一个命名参数(如我上面建议的那样)或使用 routes 将值放在 url 中的特定位置并告诉 MVC 选择哪个值来解决这个问题用于 resetType。

要使 resetType 成为您路线的一部分,请添加如下内容:

[Route("Login/{resetType}]    
public ActionResult Index(int resetType)

那你可以http://yoururl/yourcontroller/login/1

这告诉值提供者在 url 的那个位置寻找一个整数并将其分配给“resetType”。 或者,将“resetType”重命名为“id”,默认路由将为您工作。

public ActionResult Index(int id)

可选参数

如果你想让resetType 是可选的,你需要把它变成一个可以为空的int 并且改变路由到

[Route("Login/{resetType?}]    
public ActionResult Index(int? resetType)

public ActionResult Index(int? id)

【讨论】:

  • 谢谢@Franz。是的-当我尝试yoururl/yourcontroller/index?resetType=1 时,我得到了更好的结果。也谢谢你的解释。对我的理解帮助很大。我会继续修改这个。
  • 没问题。如果这最终解决了它,请不要忘记接受答案:)
【解决方案2】:

根据您的视图形式,您的帖子操作应如下所示才能使其正常工作:

[HttpPost]
public ActionResult Index(int resetType,ResetPasswordViewModel model)
{
   ........
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多