In my Model I have this list of strings:
public static List<string> listOfNames = new List<string>();
And in the Controller I have this Action:
public ActionResult NameInput()
{
NameModel.prepareList();
return View("NameInput", NameModel.listOfNames);
}
And in the top of the View I have this:
#model IEnumerable<Exercise_3.Models.NameModel>
I guess this is where it's wrong, but I don't know what to use instead. This line was OK from the beginning, when I had a list of objects, but now I just want to use a list of strings.
The error message I get when I run the application is the following:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Exercise_3.Models.NameModel]'.
The model you are sending to the view is the listOfNames property value of NameModel. The listOfNames property is of type List<string>. but in your razor view you specified the model type as IEnumerable<Exercise_3.Models.NameModel>. Those are not matching.
You may update your razor view to use List<string> as the model type
#model List<string>
<h1>Items</h1>
#foreach(var item in Model)
{
<h2>#item</h2>
}
Related
Whenever i m passing data from Controller to View this error is showing
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`6[System.String,System.String,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[CaliberCoaching.Models.CareerInformation]'.
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.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`6[System.String,System.String,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[CaliberCoaching.Models.CareerInformation]'.
This is My Controller Code
public ActionResult JobList()
{
CaliberCoachingContext objCaliberCoachingContext = new CaliberCoachingContext();
var lstCareerInformation = (from job in objCaliberCoachingContext.CareerInformations
select new { job.NameOfPost, job.PostName, job.StartDate, job.LastDate, job.Eligibility, job.NoOfVacancies }).ToList();
return View(lstCareerInformation);
}
and here is my view
#model IEnumerable<CaliberCoaching.Models.CareerInformation>
#{
ViewBag.Title = "JobList";
Layout = "~/Views/Shared/_CommonLayout.cshtml";
}
#foreach (var item in Model)
{
<div class="single-item-wrapper">
<div class="courses-content-wrapper">
<h3 class="title-default-left-bold">#item.NameOfPost</h3>
</div>
</div>
}
Please Give me the solution for this Problem.
The exception is self explanatory! Your razor view is strongly typed to a collection of CareerInformation objects, but from your action method, you are passing a different type to the view!
In your action method, the variable lstCareerInformation is not a collection of CareerInformation objects, but a collection of anonymous objects. This is happening because your LINQ expression is doing a projection to an annonymous object.
select new { job.NameOfPost, job.PostName, job.StartDate,
job.LastDate, job.Eligibility, job.NoOfVacancies }
select new will create an anonymous object
To fix the error, you should return a collection of CareerInformation objects. Just remove the projection part from your LINQ expression.
public ActionResult JobList()
{
var db = new CaliberCoachingContext();
var careerInformationList = db.CareerInformations.ToList();
return View(careerInformationList);
}
EDIT : As per the comment
i have used anonymous object because i wants to select particular
columns instead of all the columns from CareerInformation properties.
Then you should be using a view model. Create a viewmodel class with properties needed in your view.
public class JobVm
{
public string PostName { set;get;}
public Eligibility { set;get;}
public DateTime StartDate { set;get;}
// Add other properties needed by the view
}
Now in your action method, project create view model objects from your entity object collection using the Select method.
public ActionResult JobList()
{
var db = new CaliberCoachingContext();
var careerInformationList = db.CareerInformations
.Select(a=> new JobVm { PostName = a.PostName,
StartDate = a.StartDate,
Eligibility = a.Eligibility })
.ToList();
return View(careerInformationList);
}
Here careerInformationList is a list of JobVm objects and that is what we are passing to the View. So make sure your view is strongly typed to a collection of the JobVm objects.
#model List<JobVm>
<h2>Jobs</h2>
#foreach(var job in Model)
{
<div>#job.PostName</div>
<p>#job.Eligibility</p>
}
My end goal is to pass a strongly typed model (to populate a drop-down menu) and a list of models (based on a search query) to a view simultaneously from a controller. The view I am passing it to is the "Clear()" view. Currently in my HomeController.cs I have:
public ActionResult Clear()
{
var states = GetAllStates();
var model = new ProjectClearanceApp.Models.Project();
model.States = GetSelectListItems(states);
return View(model);
}
private IEnumerable<string> GetAllStates()
{
return new List<string>
{
"AL",
// ... (you get the point)
"WY",
};
}
private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<string> elements)
{
var selectList = new List<SelectListItem>();
foreach (var element in elements)
{
selectList.Add(new SelectListItem
{
Value = element,
Text = element
});
}
return selectList;
}
I read somewhere that that's the best way to get the list of options for a drop-down menu. Now I'd also like a pass a LIST of models to the same view (Clear.cshtml) based on a search query. I'm reading from this Microsoft tutorial to search in the controller action for that view by adding
public ActionResult Index(string searchString)
{
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(movies);
}
to the controller. How can I pass both the list of drop-down options AND the list of models that fit the search from the controller to the view (or, how can I achieve the same effect without passing both from the controller)?
To achieve your end goal ,that is passing the list of model, viewmodel will be the best solution as per my knowledge.
Some key note above view model:
The ViewModel class, which is the bridge between the view and the model. Each View class has a corresponding ViewModel class. The ViewModel retrieves data from the Model and manipulates it into the format required by the View. It notifies the View if the underlying data in the model is changed, and it updates the data in the Model in response to UI events from the View
The ViewModel class determines whether a user action requires modification of the data in the Model, and acts on the Model if required. For example, if a user presses a button to update the inventory quantity for a part, the View simply notifies the ViewModel that this event occurred. The ViewModel retrieves the new inventory amount from the View and updates the Model. This decouples the View from the Model, and consolidates the business logic into the ViewModel and the Model where it can be tested.
Official definition/ Source: https://msdn.microsoft.com/en-us/library/ff798384.aspx
Additional information with Ex:
http://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc
Kindly let me know your thoughts or feedbacks
Thanks
Karthik
Model is a term that really describes what data is meant to be bound to the view, and what data the model binder will handle when you make a request to/from the view. Because I'm not exactly sure how you are using Index and Clear, I'll just stub these out generally. Please let me know if I've missed the point of your question:
If you are having trouble passing, for example, List<Movies> movieModel along with your List<SelectListItem> selectedStates (or any additional values) to the view, here are 2 common ways to do this.
ViewModel approach -- Create a model class that contains your "main" model along with any other properties, lists, etc. that you'll need in the view.
eg:
public Class MyViewModelClass
{
List<Movie> MoviesList {get;set;}
List<SelectListItem> StatesList {get;set;}
//some other properties/methods can go here as well ...
}
//In Controller
{
MyViewModelClass model = new MyViewModelClass();
model.StatesList = <build your select list>;
model.MovieList = <build your movie list>;
return View(model);
}
In the controller you would then create a new instance of MyViewModelClass model (different name obviously), and populate your movie list, state list, and any other properties, assign them to the model properties, and pass the whole thing as your return View(model);
This is nice because all of the data getting passed to the view can be in one place.
ViewBag approach -- Stick with a single model or List, and pass view-related data (such as dropdown lists, or state bools) in the ViewData or ViewBag.
ex.
//From Controller
public ActionResult SomeMethod()
{
var states = GetAllStates();
var model = <enter movies query here>;
ViewBag.StatesList = GetSelectListItems(states);
//This will be accessible in the view now
return View(model);
}
//In View:
#{
List<SelectListItem> StatesList = (List<SelectListItem>)ViewBag.StatesList; // Can now use this variable to bind to DropDownList, etc.
}
I prefer, for the most part, using a ViewModel to add everything I need. For one, the ViewBag/ViewData requires some casting, and it relies on "magic strings", so you don't have Visual Studio letting you know if you'd typed something wrong with intellisense. That said, either is viable.
I have a controller that returns a list of view models, like so:
public ActionResult Index()
{
List<DailyPlanListViewModel> viewModels = new List<DailyPlanListViewModel>();
//do some stuff with the list
return View(viewModels);
}
and a view that takes the list and should display the information
#model List<IEnumerable<D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel>>
BUT I get this error, because of the IEnumerable type:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[System.Collections.Generic.IEnumerable`1[D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel]]'.
I can't get it to work. What can I do?
You need to remove the IEnumerable part:
#model List<D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel>
Your list is the IEnumerable.
A better way to approach this would be to create another class to wrap this list, so you're only passing one model back to the view instead of a list of models.
I need to have 2 models in my View. But since we could only add 1 view, i took the following approach;
#model Tuple<My.Models.Mod1,My.Models.Mod2>
#Html.DropDownListFor(m => m.Item2.humanKind,Model.Item2.allHuman)
#Html.TextBoxFor(m => m.Item1.food)
But, what i end up getting is the following error;
The model item passed into the dictionary is of type 'My.Models.Mod2', but this dictionary requires a model item of type 'System.Tuple`2[My.Models.Mod1,My.Models.Mod2]'.
What is this, and how can i solve this?
UPDATE
public ActionResult Index()
{
var model2 = new Mod2 { allHuman = allHumans() };
var model1 = new Mod1(); // JUST NOW I ADDED THIS, BUT IT DOESn't WORK
return View(model1,model2);
}
You can only have one model per view. You need to instantiate the Tuple as Ufuk suggested.
However I would suggest creating a new model that has the other models as a property.
The view in question is being called from a controller action that only passes in a My.Models.Mod2 rather than a Tuple<My.Models.Mod1,My.Models.Mod2>.
Double-check the specific controller action that calls this view.
UPDATE
Your controller code
return View(model1,model2);
should be
return View(new Tuple<My.Models.Mod1,My.Models.Mod2>(model1, model2>);
You are passing model1 and model2 as separate parameters rather than as a Tuple.
Build a view model that contains both:
Public class CompositeViewModel{
Public Mod1 mod1 {get;set;}
Public Mod2 mod2 {get;set}
}
Then construct and pass CompositeViewModel to view. Set views to use CompositeViewModel as the model #model CompositeViewModel
Using a Tuple doesn't easily allow you to expand or change what you are doing.
It maybe even looks like you have one ViewModel that has data, and then some associated IEnumerable<SelectListItem>. If that is the case then name the ViewModel like CreateAnimalTypeViewModel which contains all the properties you need to create it, then have various select lists.
If you need to map from something to the ViewModel e.g. if you were doing an edit of an existing item you could use AutoMapper.
You are not creating a tuple instance before sending it to the view.
public ActionResult Index()
{
var model2 = new Mod2 { allHuman = allHumans() };
var model1 = new Mod1();
return View(new Tuple<Mod1,Mod2>(model1,model2));
}
I am passing an object (a List) into a view like so:
private readonly CarsContext _db = new CarsContext();
public ActionResult Index()
{
ViewBag.Message = "Foobar";
var result = from cars in _db.CarProfiles
where cars.CarOfTheWeek
select cars;
return View(result.ToList());
}
How do I access this object from within my Index view? I am thinking I need to add it to my dynamic viewbag, and access it that way instead of passing it as an argument, is this correct or if I keep my controller code above as it is, can I access this object from within the view?
Thanks
Make your view strongly typed to the class/ type you are returning to, from your action method.
In your case you are returning a List of CarProfile class objects. So in your view(index.cshtml), drop this as the first line.
#model List<CarProfile>
Now you can access the list of CarProfile using the keyword Model.for example, you want to show all items in the list, in a loop, you can do like this
#model List<CarProfile>
#foreach(var item in CarProfile)
{
<h2>#item.Name</h2>
}
Assuming Name is a property of CarProfile class.
In your Index.cshtml view, your first line would be:
#model YOUR_OBJECT_TYPE
So for example, if you pass a string in your line:
return View("hello world");
Then your first line in Index.cshtml would be:
#model String
In your case, your first line in Index.cshtml would be:
#model List<CarProfile>
This way, if you type #Model in your Index.cshtml view, you could access to all the properties of your list of CarProfile. #Model[0] would return your first CarProfile in your list.
Just a tip, the ViewBag shouldn't be used most of the time. Instead, you should create a ViewModel and return it to your view. If you want to read about ViewModels, here's a link: http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3fundamentals_topic7.aspx
You can access the object in your view by passing it in ViewBag ( The way that you have wrote your controller ). But I think, it is usually better to have a ViewModel and pass data to view, via that ViewModel.
Passing Business objects directly to model ( the way you have done ), make model dependent to business object, that it is not optimal in the most cases. Also having ViewModel instead of using ViewBag has the advantage of using StrongTypes.
private readonly CarsContext _db = new CarsContext();
public ActionResult Index()
{
var result = from cars in _db.CarProfiles
where cars.CarOfTheWeek
select cars;
MyViewModel myViewModel = new MyViewModel( );
myViewModel.Message = "Foobar";
myViewModel.ResultList = result.ToList();
return View( myViewModel );
}