Skip to content

Ambiguous Error in ViewModel

January 18, 2016

If you ever receive the common MVC error when converting a returning object to the correct type which occurs during runtime say for a page called OrderSummary.

The current request for action ‘OrderSummary’ on controller type ‘HomeController’ is ambiguous between the following action methods:
System.Web.Mvc.ActionResult OrderSummary() on type WebApplication.Controllers.HomeController
System.Web.Mvc.ActionResult OrderSummary(System.Collections.Generic.List`1[WebApplication.Models.OrderViewModel]) on type WebApplication.Controllers.HomeController

The problem here is that the object OrderViewModel is not being passed correctly as a list of enumerable type that the View Model can read in, you can set this like so: –

var convertedList = (from db in onlineOrders.AsEnumerable()
 select new Order()
 {
 Id = Convert.ToInt32(db["Id"]),
 ItemID = Convert.ToInt32(db["ItemID"]),
 CustomerID = Convert.ToInt32(db["CustomerID"]),
 DateOrdered = (emp["DateOrdered"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(db["DateOrdered"])),
 Quantity = Convert.ToInt32(db["Quantity"])
 }).ToList();
List<OrderList> convertedList = new List<OrderList>();
var emptyList = convertedList;
return View(convertedList);

You notice here that the convertlist is using the enumerable type of ToList () which convert the the database read items from the table onlineOrders as enumerable, rather than casting it manually which doesn’t work, and then I add this converted enumerable list to be placed in var that is read to the ViewMoel called OrderSummary.

Hope that helps!!!!!!

 

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: