pass two models to view [duplicate] - c#

This question already has answers here:
ASP.NET MVC - View with multiple models
(4 answers)
Closed 9 years ago.
I am new to mvc and try to learn it by doing a small project with it. I have a page which is supposed to display that specific date's currencies and weather. so I should pass currencies model and weather model. I have done to pass currencies model and works fine but I dont know how to pass the second model. And most of the tutorials on the shows how to pass only one model.
can you guys give an idea how to do it.
this is my current controller action which sends currency model
public ActionResult Index(int year,int month,int day)
{
var model = from r in _db.Currencies
where r.date == new DateTime(year,month,day)
select r;
return View(model);
}

You can create special viewmodel that contains both models:
public class CurrencyAndWeatherViewModel
{
public IEnumerable<Currency> Currencies{get;set;}
public Weather CurrentWeather {get;set;}
}
and pass it to view.
public ActionResult Index(int year,int month,int day)
{
var currencies = from r in _db.Currencies
where r.date == new DateTime(year,month,day)
select r;
var weather = ...
var model = new CurrencyAndWeatherViewModel {Currencies = currencies.ToArray(), CurrentWeather = weather};
return View(model);
}

You have to create a new model which has to contain the whole objects that you want to pass it to view. You should create a model (class, object) which inherits the base model (class, object).
And other suggestion you may send objects (models) via View["model1"] and View["model2"] or just an array that contains objects to pass it and cast them inside the view which I don't advise .

It sounds like you could use a model that is specific to this view.
public class MyViewModel{
public List<Currencies> CurrencyList {get;set;}
}
and then from your controller you could pass this new View Model into the view instead:
public ActionResult Index(int year,int month,int day)
{
var model = from r in _db.Currencies
where r.date == new DateTime(year,month,day)
select r;
return View(new MyViewModel { CurrencyList = model.ToList() });
}
You can than just add more properties to your view model which contain any other models (Weather model) and set them appropriately.

Related

Issue with using two models in the view in ASP.NET MVC [duplicate]

This question already has answers here:
Two models in one view in ASP MVC 3
(12 answers)
Closed 4 years ago.
I am trying to use two models in my view, but I am get an error saying that only one model can be used. I get the purpose of why only one model can be used but are there any workarounds so I could use two models in my view. I have listed the code below.
Controller:
/* Parts Method */
public ActionResult Parts()
{
return View(db.Part.ToList());
}
View:
#model Inventory_System.Models.Transaction
#model IQueryable
The first model is for connecting to the view model and the second is connecting to a database list from the controller to be displayed. How could I use both of these models in the same view?
You should create a composite model with two properties.
View Model
public class CompositeModel
{
public Transaction Transaction {get;set}
public List<Part> ListOfParts {get;set}
}
Controller
public ActionResult Parts()
{
CompositeModel model = new CompositeModel
{
Transaction = new Transaction();
ListOfParts = db.Part.ToList();
};
return View(model);
}
View
#model /*Name Space*/.CompositeModel;
I think that is good solution.
You should create a ViewModel that contains your two models
public class ViewModel()
{
public Inventory_System.Models.Transaction Transaction {get;set;}
public IQueryable<Inventory_System.Models.Part> Part {get;set;}
}

MVC - Populate Two Dropdowns From Database in Partial View with ViewModel

Here's the visual of what I'm trying to do.
So basically what happens here is that you choose a name from the select on the left and a team from the select on the right, then click Submit Assignment and it adds a record. (This information is for reference so you can understand the context. I don't need help with the database inserts or updates.)
This is going to be a PartialView with a ViewModel.
I need to know how to populate those two dropdowns with separate lists. I'm assuming those would be coming over in the ViewModel.
So in my main view, I have a call to the PartialView with RenderAction.
//The action //The controller
#{Html.RenderAction("TeamAssignment", "TeamAssignment");}
The controller that gets called
public class TeamAssignmentController : Controller
{
MyDatabaseEntities db = new MyDatabaseEntities();
[ChildActionOnly]
public ActionResult TeamAssignment()
{
//NEED A VIEW MODEL HERE TO PASS INTO THE PARTIALVIEW
return PartialView();
}
}
In the ViewModel I will need lists that populate the two drop downs.
The linq query on the left for Name would be this.
var nameList = (from p in db.Participant
where p.ParticipantType == "I"
select new {
p.ParticipantID,
p.IndividualName
}).ToList();
The linq query on the right for Team would be this.
var teamList = (from p in db.Participant
where p.ParticipantType == "T"
select new {
p.ParticipantID,
p.TeamName
}).ToList();
How can I pass that information into the PartialView and how do I populate the dropdowns with the ID for value and name for display text?
UPDATE
I belive this may be the ViewModel I need. Is this correct?
public class TeamAssignmentViewModel
{
//Need these ints for the updating and deleting
public int IndividualParticipantID { get; set; }
public int TeamParticipantID { get; set; }
public List<Participant> NameList { get; set; }
public List<Participant> TeamList { get; set; }
}
You need to create an instance of a viewmodel, populate it and pass it to your PartialView:
[ChildActionOnly]
public ActionResult TeamAssignment()
{
// Instantiate A VIEW MODEL HERE TO PASS INTO THE PARTIALVIEW
MyCustomModel model = new MyCustomModel();
model.nameList = (from p in db.Participant
where p.ParticipantType == "I"
select p).ToList();
model.teamList = (from p in db.Participant
where p.ParticipantType == "T"
select p).ToList();
return PartialView("TeamAssignment", model);
}
EDITED: removed use of dynamic class in linq statements
The markup for the Dropdowns might look something like:
#Html.DropDownListFor(model => model.IndividualParticipantID,
new SelectList(Model.NameList, "ParticipantID", "FirstName")
#Html.DropDownListFor(model => model.IndividualParticipantID,
new SelectList(Model.TeamList, "ParticipantID", "Team")
I'll start by admitting that I haven't read everything posted, but I can help with passing data to the PartialView to be used as a model.
In your View, you can build the partial like this:
#Html.Partial("_PartialViewName", currentItem,
new ViewDataDictionary {
{ "ExtraValue", Model.Count },
{ "SecondExtraValue", #ViewBag.ValueToPass });
Then in the PartialView, you can do this:
#model [Type of currentItem from above]
#{
// I'm using var here for shorthand, whereas I do strong typing and cast in my codebase.
var dataValue = ViewData["ExtraValue"];
var secondDataValue = ViewData["SecondExtraValue"];
}
Using this method you can pass in a model, as well as other values that may be needed for rendering.
Hope this helps. :)

Interaction between Models and ViewModels in ASP.NET MVC

Could you explain the interaction Models and ViewModels in the ASP.NET MVC?
If I need to display data on the page, but not edit, whether to create a ViewModel to display or use the Model?
I have two methods in the repository. One returns the Model and the other Model gets.In View I need to send the model. Should I convert the resulting Model to a ViewModel that would pass it to the View, and upon receipt of the submission to convert it back into the model to keep it?
For example I have a class model and class ViewModel.
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
public class EditItemItemViewModel
{
public string Name { get; set; }
public int Price { get; set; }
}
On the edit page, I clicked the edit item, and must get to the controller:
[HttpGet]
public ActionResult EditItem(int id)
{
//some code
return View();
}
Where can I get the ID if I passed in the view ViewModel in which there was no ID?
If I somehow got the ID, I need to do the following, which would save the model?
[HttpPost]
public ActionResult EditItem(EditItemItemViewModel ViewModel)
{
var Item = _dataManager.Items.GetItemById("1");
Item.Name = ViewModel.Name;
Item.Price = ViewModel.Price;
_dataManager.Items.AddItem(Item);
return View("Sucsess");
}
Could you tell me how to work with Models and ViewModels?
You can get the id a few different ways:
#Html.HiddenFor(m => m.Id)
This will include the property in the actual HTTP request.
Another option is to simply include the id as part of your route (this is what I usually do):
#Html.BeginForm("EditItem", "SomeController", new { Id = Model.Id }, FormMethod.Post) {
...
}
In both instances, make sure to validate that the user should be able to update the record that corresponds to that id! There's nothing stopping a user from tampering with the id and sending you a different value.
As for whether or not to display the database model or the view model, that's really up to you. I would always advocate building a view model and keep your database models out of the picture except for in your controller. The convention I use at work is for every database object that I need to send to users I will create a corresponding view model. So if I have a database object called Product I will build another class for the view called ProductModel.
An advantage of following that pattern is something I actually explained to another user earlier in regards to model binding.
If I need to display data on the page, but not edit, whether to create a ViewModel to display or use the Model?
If it's very simple (like your example) and the properties map 1-1 like they do now. I would just use the Model as a view model, it's easier. Though if you want you could create a view model with the exact same properties and populate that and display it..it's a bit more work, but makes it so your domain models aren't necessarily tied to your views.
If you do that you may want to look into something like AutoMapper which would allow you to do something like
//simply copies the properties from one type to another
var viewModel = Mapper.Map<Item, EditItemItemViewModel>();
I have two methods in the repository. One returns the Model and the other Model gets.In View I need to send the model. Should I convert the resulting Model to a ViewModel that would pass it to the View, and upon receipt of the submission to convert it back into the model to keep it?
If you go the view model route then yes, you will end up doing a lot of converting between model and viewmodel, that's where something like AutoMapper can help out, or you can just create a couple extension methods that convert between the two types.
It looks like Justin Helgerson's answer explaining a way to handle models/viewmodels is pretty good.
Your posting your viewmodel, so i'm going to assume that you've referenced your model within the page
#model MyNameSpace.EditItemItemViewModel
Is there a reason the id is not included in your view model? The easiest method would be to include that in the model and pass the instantiated model when creating the view.
[HttpGet]
public ActionResult EditItem(int id)
{
var myViewModel = new EditItemItemViewModel() { Id = id };
return View(myViewModel);
}
Like Justin said, it is easier to put it in a hidden field somewhere inside the #Html.BeginForm as the id should just be for reference.
[HttpPost]
public ActionResult EditItem(EditItemItemViewModel viewModel)
{
var Item = _dataManager.Items.GetItemById(viewModel.Id);
Item.Name = viewModel.Name;
Item.Price = viewModel.Price;
// Should this be an Add?
_dataManager.Items.AddItem(Item);
return View("Success");
}

ASP.NET MVC Passing DataTime to View a

I have a asp.net MVC4 web project it shows a list of production data for that day. I have added a datetime picker which allows the user to select a date that they want to show information for.
The problem i am having is i am not sure how to go about passing the information back to the view from the method i have inside the controller.
I have the date passing back to the controller. Inside the controller i am doing a LINQ statement that allows me to select only the production data for that day.
[HttpPost]
public ActionResult GetProductionDateInfo(string dp)
{
DateTime SelectedDate = Convert.ToDateTime(dp);
DateTime SelectedDateDayShiftStart = SelectedDate.AddHours(7);
DateTime SelectedDateDayShiftEnd = SelectedDate.AddHours(19);
var ProductionData =
from n in db.tbl_dppITHr
where n.ProductionHour >= SelectedDateDayShiftStart
where n.ProductionHour <= SelectedDateDayShiftEnd
select n;
return View();
I am looking to get the Var ProductionData passed back to the view so that display it inside a table.
You can return ProductionData directly to your View.
return View(productionData)
And then in your View you could have #model IEnumerable<Type>
However, a better practice would be to create a strongly typed ViewModel to hold the ProductionData and then return the following:
var model = new ProductionDataViewModel();
model.Load();
return View(model);
Where model a definition as follows:
public class ProductionDataViewModel {
public List<ProductionDataType> ProductionData { get; set; }
public void Load() {
ProductionData = from n in db.tbl_dppITHr
where n.ProductionHour >= SelectedDateDayShiftStart
where n.ProductionHour <= SelectedDateDayShiftEnd
select n;
}
}
Then in your view use the new strongly typed ViewModel:
#model ProductionDataViewModel
Use a model, something like:
public class ProductionDataModel
{
//put your properties in here
public List<ProductionData> Data { get; set; }
}
Then create/return it in your ActionResult:
var ProductionData =
from n in db.tbl_dppITHr
where n.ProductionHour >= SelectedDateDayShiftStart
where n.ProductionHour <= SelectedDateDayShiftEnd
select new ProductionData
{
//set properties here
};
var model = new ProductionDataModel
{
Data = ProductionData
};
return View(model);
Then in your view, set your model at the top:
#model ProductionDataModel
Your ProductionData variable should now be of type IEnumerbable<tbl_dppITHrRow>.
You can pass in the model from your controller using this code at the bottom of your action:
return View(ProductionData);
In your view, you can make this your model type by placing the following Razor code in your view's .cshtml file:
#model IEnumerbable<tbl_dppITHrRow>
Then, you can use your model in your view code:
#foreach(var row in Model) {
<div>#row.Value</div>
}
The problem here is that you are returning nothing to your view here return View(); this view just render view and no data will be passed to it.
if ProductionData is getting values then
return return View(ProductionData);
You can then use the values passed in the view.

Getting error attempting to add multiple models to a view

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));
}

Categories

Resources