ASP.Net MVC routing issue with parameters - asp.net-mvc

This is my Action method:
public ActionResult Index(int SelectedID, int mode)
{
ViewModel viewModel = new ViewModel();
viewModel.SelectedID = SelectedID;
viewModel.Mode = mode;
return View(viewModel);
}
This is how I call it:
localservername/DManager/DManager/Index?SelectedID=9306270318&Mode=DataManager
And I am getting the following error:
The parameters dictionary contains a null entry for parameter 'SelectedID' of non-nullable type 'System.Int32' for method
'System.Web.Mvc.ActionResult Index(Int32, Int32)' in
'MscanES.Web.Areas.DManager.Controllers.DManagerController'. An
optional parameter must be a reference type, a nullable type, or be
declared as an optional parameter.
Parameter name: parameters
Clueless..

C# int (Int32) range is -2,147,483,648 to 2,147,483,647. Your number 9,306,270,318 is obviously over the range, use Int64 instead.

Related

operator '==' cannot be applied to operands of type 'int?' and 'object' ASP.NET MVC

I need to select data from database depending on Session value , but I'm getting this error always
'operator '==' cannot be applied to operands of type 'int?' and 'object' MVC '
I tried the following code in orders controller and orders view:
public ActionResult ordersCash()
{
return View(db.Lab_orders_Cash.Where(x => x.patient_no == Session["UserpatientNo"]).ToList());
}
I tried the solutions in this site by using (int) but its not working :
public ActionResult ordersCash()
{
return View(db.Lab_orders_Cash.Where(x => x.patient_no == (int)Session["UserpatientNo"]).ToList());
}
When i used (int)Session["UserpatientNo"]) i got this error :
LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression.
Without where clause i can get all data but i need to filter and search by session value.
How can i solve this error?
Pull the session variable into a variable:
public ActionResult ordersCash()
{
int userPatientNum = (int)Session["UserpatientNo"];
return View(db.Lab_orders_Cash.Where(x => x.patient_no == userPatientNum).ToList());
}

Spring.NET AOP logging optional parameter issue

I'm using the setup described in this SO answer: Unobtrusive AOP with Spring.Net and source based on q9114762_aop_on_mvc_controllers on GitHub to get AOP logging for my controllers.
All is great except when a controller method with optional parameters is invoked without the optional parameter.
My test methods:
using System.Web.Mvc;
public class OrderController : Controller
{
[SetMethodInfoAsMessage]
virtual public ActionResult Test1()
{
return null;
}
[SetMethodInfoAsMessage]
virtual public ActionResult Test2(int someParam = 0)
{
return null;
}
[SetMethodInfoAsMessage]
virtual public ActionResult Test3(int? someParam = 0)
{
return null;
}
[SetMethodInfoAsMessage]
virtual public ActionResult Test4(int someParam)
{
return null;
}
}
and here's the corresponding behaviour when GETting these methods from a browser:
http://localhost:62376/Order/Test1 - 200 OK
http://localhost:62376/Order/Test2 - 500 Internal Server Error. Server Error in '/' Application. The parameters dictionary contains an invalid entry for parameter 'someParam' for method 'System.Web.Mvc.ActionResult Test2(Int32)' in 'InheritanceAopProxy_7b93ae81d25d46529bebc7ed00ebc409'. The dictionary contains a value of type 'System.Reflection.Missing', but the parameter requires a value of type 'System.Int32'. Parameter name: parameters
http://localhost:62376/Order/Test2?someParam=15 - 200 OK
http://localhost:62376/Order/Test3 - 500 Internal Server Error. Server Error in '/' Application. The parameters dictionary contains an invalid entry for parameter 'someParam' for method 'System.Web.Mvc.ActionResult Test3(System.Nullable1[System.Int32])' in 'InheritanceAopProxy_7b93ae81d25d46529bebc7ed00ebc409'. The dictionary contains a value of type 'System.Reflection.Missing', but the parameter requires a value of type 'System.Nullable1[System.Int32]'. Parameter name: parameters
http://localhost:62376/Order/Test3?someParam=15 - 200 OK
http://localhost:62376/Order/Test4 - 500 Internal Server Error. Server Error in '/' Application. The parameters dictionary contains a null entry for parameter 'someParam' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Test4(Int32)' in 'InheritanceAopProxy_7b93ae81d25d46529bebc7ed00ebc409'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
The last test (6.) makes sense since someParam is mandatory. But we can see from 2. and 4. that not supplying an optional parameter results in a 500. Another thing to note is the error text from 4. - An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. So according to this, 4. should work, right? (since Test3's optional parameter is nullable).
Has anyone else experienced this? Anyone have any suggestions as to a workaround (other than manually adding logging statements to the methods) ?
This is using Spring.Aop 1.3.2, Spring.Core 1.3.2, Spring.Web. 1.3.2 and Spring.Web.Mvc3 1.3.2.
EDIT: as requested, here's the advice. It simply logs out args except for passwords (we don't want those logged):
public class SetMethodInfoAsMessageAdvice : IMethodBeforeAdvice //, IThrowsAdvice
{
private static readonly PELogger log = Log4NetHelper.GetLogger(typeof(SetMethodInfoAsMessageAdvice));
public void Before(MethodInfo m, object[] args, object target)
{
ILog logger = LogManager.GetLogger(m.DeclaringType);
string dump = args.ToJson();
if (dump.Contains("password", StringComparison.OrdinalIgnoreCase))
dump = "<password hidden>";
logger.Info(m.Name + "(" + dump + ")");
}
}

Getting an error using Backbone.js DELETE with ASP.Net MVC. Everything else works though.

I am able to successfully call Backbone's HTTP POST and PUT methods and have them link up to my server using Asp.Net MVC.
The problem is that when I call the HTTP DELETE using model.destroy() I get this error...
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Delete(Int32)' in 'GSASF.Controllers.AdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Right before I call model.destroy() I logged the id to the console and it was correct. This is my code. *Note that my model doesn't have a field called id but instead a field called HoverId. The database table already existed so I have to make due.
Delete: function(id) {
if (id) {
for (var i = this.collection.length - 1; i >= 0; i--) {
var item = this.collection.at(i);
if (item.get("HoverId") == id)
alert("Item to be destroyed ID: " + item.get("id"));
item.destroy();
};
}
[ActionName("SpaceToolTipEdit")]
[HttpDelete]
public ActionResult Delete(int id)
{
var imageHover = sr.GetImageHoverById(id);
if (imageHover != null)
{
sr.DeleteImageHover(imageHover);
return new HttpStatusCodeResult(200);
}
return new EmptyResult();
}
Ok, I feel dumb now.
The solution was all to simple. I had originally set the url attribute to my /{Controller}/{SpaceToolTipEdit} url. I was supposed to set this to the urlRoot attribute instead.

How to Handle Variable Number of Parameters in Route Method

I want to use the same post Action method for multiple number of input fields. How can I get hold of all the parameters passed to MVC engine (in addition to the method parameter)?
e.g. Both these list of values should be handled by the method below
1,A,A
1,B,A,A
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int id)
{
//for each variable in POST store in DB
}
You can access the Request.Form collection.
foreach (string field in Request.Form) {
string name = field;
string value = Request.Form[field];
//...
}

Is this an ASP.NET MVC 2 Preview 1 bug, and what's the work-around?

I'm using this code in ASP.NET MVC 2 preview 1:
public ActionResult List(long id, [DefaultValue(0)] long? commentId)
{
var model = UserComment.ForRefId(id);
PageTitle = string.Format("Comments for '{0}'",
SetCommentThread(id).Subject);
ViewData[MvcApplication.SAnchor] = commentId;
return View("List", model);
}
When I use a valid URL argument such as "/Comment/List/22638", I get the error:
The parameters dictionary contains an
invalid entry for parameter
'commentId' for method
'System.Web.Mvc.ActionResult
List(Int64,
System.Nullable1[System.Int64])' in
'ThreadsMVC.Controllers.CommentController'.
The dictionary contains a value of
type 'System.Int32', but the parameter
requires a value of type
'System.Nullable1[System.Int64]'.
Parameter name: parameters
If I change the declaration to:
public ActionResult List(long id, [DefaultValue(0)] int? commentId)
The code runs fine. Is this something I'm doing wrong, or a problem with the reflection being too type strict for Int32 vs Int64? And what can I do to fix it? Cast the long as a string?
I think this is more readable/less messy, and works in MVC 1, too:
public ActionResult List(long id, long? commentId)
{
var model = UserComment.ForRefId(id);
PageTitle = string.Format("Comments for '{0}'",
SetCommentThread(id).Subject);
ViewData[MvcApplication.SAnchor] = commentId.GetValueOrDefault(0);
Try
public ActionResult List(long id, [DefaultValue((long)0)] long? commentId)
Just add the following to the global.asax Application_Start method
ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();
For more on this, see Scott's blog: http://weblogs.asp.net/scottgu/archive/2010/12/14/update-on-asp-net-mvc-3-rc2-and-a-workaround-for-a-bug-in-it.aspx
Also see jQuery Sort and MVC Stopped Working

Resources