I see this error on my page
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[sucu1.Entities.Inventory]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable1[sucu1.Models.InventoryViewModel]'.
Related
I want to pass a object as model to a partial view in umbraco. there is two function #Html.Partial() and #Html.Partial() which have 4 override method listed below :
#Html.Partial(string partialName)
#Html.Partial(string partialName, Object object)
#Html.Partial(string partialName, Object object, ViewDataDictionary dic)
#Html.Partial(string partialName, ViewDataDictionary dic)
and :
#Html.RenderPartial(string partialName)
#Html.RenderPartial(string partialName, Object object)
#Html.RenderPartial(string partialName, Object object, ViewDataDictionary dic)
#Html.RenderPartial(string partialName, ViewDataDictionary dic)
I try to use #Html.RenderPartial(string partialName, Object object) and
#Html.Partial(string partialName, Object object) but i an getting this Exception:
Cannot bind source type <>f__AnonymousType0`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to model type Umbraco.Web.Models.RenderModel.
I search a lot in google to an example of using these methods but i cannot find any thing. so there are two question:
What is different between Partial and RenderPartial method?
How can I pass data to partialview from page razor?
I found solution. my PartialView was inherited from :
Umbraco.Web.Mvc.UmbracoTemplatePage
now i change its inheritance to :
#inherits Umbraco.Web.Mvc.UmbracoViewPage<MyModel>
and then I can add mymodel object to RenderPartial() Method.
I am using Entity Framework code first with a generic repository pattern with ASP.NET MVC. I have two tables Category and Product.
My model class of product is like this
Public class Product
{
public int ProductID{get;set;}
Public int CategoryID{get;set;}
[ForeignKey("CategoryID")]
public virtual Category Category{get;set;}
[NotMapped]
public string CategoryName{get;set;}
}
The model is binding correctly as long as I am getting data using DBContext.
But I am having a problem when I am getting list of products from stored procedure mapped to Product object. So it is not mapping the Category property of Product object and hence I cannot able to get Category.CategoryName.
So I added a new property with [NotMapped] attribute in product class as CategoryName. But it is also not binding from stored procedure.
And if I remove the [NotMapped] attribute then it is correctly binding from stored procedure but error occurs again when getting product by DbContext (Linq).
Please help me in this regards.
You don't need to add an extra property, use the DbSet.SqlQuery method for queries that return entity types. The returned objects must be of the type expected by the DbSet object, and they are automatically tracked by the database context unless you turn tracking off.
var products= _context.Products.SqlQuery("storedProcedureName",params);
The columns returned by SP should match the properties of your entity type otherwise, it will throw an exception.
After execute your SP, you should be able of get the CategoryName through your Category navigation property:
var catName=someProduct.Category.CategoryName;
On the other hand, the returned data by the Database.SqlQuery isn't tracked by the database context, even if you use this method to retrieve entity types. If you want to track the entities that you get after execute your SP using this method, you can try this:
//Attach the entity to the DbContext
_context.Product.Attach(someProduct);
//The Category navigation property will be lazy loaded
var catName=someProduct.Category.CategoryName;
If you have disabled lazy loading you can load explicitly your navigation property:
//Load the Category navigation property explicitly
_context.Entry(someProduct).Reference(c => c.Category).Load();
I want to use a view with model as IEnumerable type some thing like this
#model IEnumerable<WCG.Data.EntityObjects.FaxHistory>
Note:-I have classes as FaxHistory.cs(Main class where variables are declared)
FaxHistoryBo.cs
FaxHistoryDao.cs
IfaxHIstoryDao.cs
FaxHistoryController.cs
FaxHistorymodel.cs
I am using mvc 3 razor and problem is recording the dropdownlist value in database using razor helpers:
#Html.DropDownListFor(m => m.Question, (IEnumerable<SelectListItem>)ViewBag.QuestionList)
Here, My view is using model binding. while in database the question column is of sting data type (varchar) and while running application it shows following erros after submitting form
The ViewData item that has the key 'Question' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
What should I change here to avoid erros here I have to use model binding.
What should I change here to avoid erros here I have to use model binding.
You should ensure that inside the controller action that rendered this view you have populated the ViewBag.QuestionList property with an IEnumerable<SelectListItem>. People usually forget to do this in their POST actions when redisplaying the same view containing this DropDown:
IEnumerable<SelectListItem> items = ...
ViewBag.QuestionList = items;
return View(someModel);
Also make sure that the Question property on your model is a scalar type (string, integer, ...) and not a complex type. If it is a complex type you need to select the corresponding scalar property to bind the selected value to:
#Html.DropDownListFor(
m => m.Question.QuestionId,
(IEnumerable<SelectListItem>)ViewBag.QuestionList
)
I lack understanding of some basic MVC concepts, despite all my searching.
I created an MVC project in Visual Studio, which contains the partial view _LogOnPartial.shtml. I just want to access information within the view pertaining to the user, to put in a user dropdown menu. When I try to put this at the top of the partial view cshtml page I get the above error:
#model MyProject_MVC.Models.UserRepository
When I try this I also get an error:
#Html.Partial("_LogOnPartial", MyProject_MVC.Models.UserRepository)
'MyProject_MVC.Models.UserRepository' is a 'type', which is not valid in the given context
You have to provide an instance of MyProject_MVC.Models.UserRepository to the partial view. _LogOnPartial is strongly-typed to that type. It lets you access its public members at compile time and decide how you can display it in your view.
If you want to use your own type in that view, first you have to change the type that is strongly-typed to it.
#model MyProject_MVC.Models.UserRepository
Then you have to create an instance of that type in your action method and pass it to the view as the model or a property of the model.
public ActionResult LogOn()
{
return View(new UserRepository());
}
Now you can access this instance as Model object in your view.
#Html.Partial("_LogOnPartial", Model);
#model _LogOnPartial.Models.UserRepository should probably be: #model MyProject_MVC.Models.UserRepository
and for the last part, you have to provide and instance of the type UserRepository as the second parameter, not the type itself.