Jennifer
Jennifer

Reputation: 1

View Error The parameters dictionary contains a null entry for parameter

I'm working on my first MVC5 app and am running into the following error when trying to preview a view:

"The parameters dictionary contains a null entry for parameter 'resetType' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'Morpheus.Controllers.ResetPasswordController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters"

I understand that somewhere in my view I need to pass a parameter. This is because my view is one of two that will be linked to from another view. The final URL will be: /ResetPassword?resetType=1. I don't know how to pass resetType = 1 to the controller method. I tried it in the Razor form tag and per a hidden input per this post: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'

Any help would be greatly appreciated. Thanks so much. Please let me know if I can post more of my code.

View Code:

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

Controller:

 public ActionResult Index(int resetType)
        {
            return View(new ResetPasswordViewModel() { ResetType = resetType });
        }

Adding Model Code:

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

Upvotes: 0

Views: 5046

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

According to your view form,your post action should look like this to make it work:

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

Upvotes: 1

flytzen
flytzen

Reputation: 7438

The problem is not in your View code, it's in the controller, specifically this:

public ActionResult Index(int resetType)

If you do this in your browser http://yoururl/yourcontroller/index?resetType=1 you should see a better result.

What's happening is that you are saying your controller must be passed an integer (it's not an optional parameter). The ModelBinder will try to see if it can find a "resetType" in any of the data that is passed in the request, like in the URL or any form data. The ModelBinder can't find that so it complains that it cannot supply a value for resetType to your controller. You can solve this by either specifying a named parameter in the query string (as I suggested above) or use the routes to put the value in a specific position in the url and tell MVC which value to pick out to use for resetType.

To make resetType part of your route, add something like this:

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

then you can http://yoururl/yourcontroller/login/1

This tells the value provider to look for an integer in that position in the url and assign it to "resetType". Alternatively, rename "resetType" to "id" and the default route will work for you.

public ActionResult Index(int id)

Optional parameter

If you want resetType to be optional, you need to make it a nullable int and change the route to

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

or

public ActionResult Index(int? id)

Upvotes: 2

Related Questions