eoinDeveloper
eoinDeveloper

Reputation: 103

C#.NET MVC4 - null entry for parameter of non-nullable type for a method

Within my c#.net project I am getting this error when I try to get an onClick event to begin. I think I may be missing an important assembly reference but cannot figure out why this error is occurring. The project was working fine up until recently and some modification I have made has caused this problem

Issue

SERVER ERROR IN APPLICATION

Server Error in '/' Application.

The parameters dictionary contains a null entry for parameter 'lat' of non-nullable type 'System.Double' for method 'System.Web.Mvc.ActionResult Index(Double, Double, System.String, System.String, System.String, System.String, System.String)' in 'MyDisplayTracking.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'lat' of non-nullable type 'System.Double' for method 'System.Web.Mvc.ActionResult Index(Double, Double, System.String, System.String, System.String, System.String, System.String)' in 'MyDisplayTracking.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: The parameters dictionary contains a null entry for parameter 'lat' of non-nullable type 'System.Double' for method 'System.Web.Mvc.ActionResult Index(Double, Double, System.String, System.String, System.String, System.String, System.String)' in 'MyDisplayTracking.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters] System.Web.Mvc.ActionDescriptor.ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary2 parameters, MethodInfo methodInfo) +664911 System.Web.Mvc.<>c__DisplayClass1.<Execute>b__0(ParameterInfo parameterInfo) +18 System.Linq.WhereSelectArrayIterator2.MoveNext() +85 System.Linq.Buffer1..ctor(IEnumerable1 source) +217 System.Linq.Enumerable.ToArray(IEnumerable1 source) +78 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +133 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +27 System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28 System.Web.Mvc.Async.<>c__DisplayClass81.b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +50 System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +237 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +12 System.Web.Mvc.Async.WrappedAsyncResult1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50 System.Web.Mvc.Async.<>c__DisplayClass2a.b__20() +24 System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult asyncResult) +126 System.Web.Mvc.Async.WrappedAsyncResult1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +45 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +61 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +25 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +49 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +49 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9042429 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1016

\

Home controller:

 public ActionResult MyStores(double lat, double lng, string storename, string city, string state, string distributor, string channels)
    {
        MyStoresModel r = new MyStoresModel();
        r.stores = new List<StoreDataModel>();
        using (DisplayTrackingEntities ctx = new DisplayTrackingEntities())
        {

and I am trying to call it from my Index view as follows:

            <input type="hidden" id="lat" name="lat" />
        <input type="hidden" id="lng" name="lng" />
        <div class="row">
            <div class="form-actions">
                <button class="btn" onclick="return UseGPS();">Use GPS</button>
                <button class="btn" onclick="return Search();">Search</button>
            </div>
        </div>

Upvotes: 2

Views: 2090

Answers (2)

Gopher
Gopher

Reputation: 79

You could assign default values to lat & lng in your action's parameter list like this:

public ActionResult MyStores(double lat = 0.0, double lng = 0.0, string storename...

If these fields have values on the page, they'll post back and override the defaults. If they don't have values, they'll not post back (hence the null), so the fields will retain the default. Basically, these fields are now optional parameters.

Upvotes: 1

John H
John H

Reputation: 14655

You're getting the System.ArgumentException because you haven't assigned values to either of the hidden fields. Your view should look something like this:

<input type="hidden" id="lat" name="lat" value="1.234" />
<input type="hidden" id="lng" name="lng" value="5.678" />

Upvotes: 2

Related Questions