View with Many to many relationship - asp.net-mvc

I came across the following situation:
I have 2 tables:
Eventos and Convidados
I had to create a third table to associate multiple
id_evento and id_convidado
I've created a controller called EventoConvidado and need to display on the screen all Convidados.
How I do to pass the model to the View?

I found only one question in your description - how to pass a model to a view.
You create a view model called EventosConvidadosViewModel like this:
public class EventosConvidadosViewModel
{
public Eventos Eventos { get; set; }
public Convidados Convidados { get; set; }
}
In the controller you do something like this:
public ActionResult Index()
{
var viewModel = new EventosCandidatosViewModel();
// should you need to fill in the parameters for the view model you do it here
return View(viewModel);
}
In the view your first line should be
#model EventosCandidatosViewModel;
You use this model inside the view in the following manner: #Model. You access its properties like this: #Model.Eventos or #Model.Convidados.
Hope this helps you.

Related

MVC multiple tables for one view

I have a view which contains data from 4 tables from the database.
Do i need to define all of those fields in my model? so that i can use them in my view like :-
#model.fieldFromTable1
#model.fieldFromTable2
#model.fieldFromTable3
#model.fieldFromTable4
The quick answer is yes. You can probably think of your model more as a ViewModel. Not an actual model from your database.
In your controller you would just populate the ViewModel from your database models
MyViewModel.cs
//put whatever properties your view will need in here
public class MyViewModel
{
public string PropertyFromModel1 {get; set;}
public string PropertyFromModel2 {get; set;}
}
MyController.cs
public ActionResult MyAction()
{
//the only job your action should have is to populate your view model
//with data from wherever it needs to get it
Model1 model = GetFirstModelFromDatabase();
Model2 model2 = GetSecondModelFromDatabase();
MyViewModel vm = new MyViewModel
{
PropertyFromModel1 = model.MyProperty;
PropertyFromModel2 = model2.MyProperty;
}
return View(vm);
}
MyAction.cshtml
#Model.PropertyFromModel1
#Model.PropertyFromModel2
It's actually pretty standard practice to not use your raw domain models in your views, because I would say that typically don't match up exactly to what you want to display.

How to provide a model to a nested view in MVC (Razor)

Suppose I'm rendering another page(view) to a page (view).
Now the nested view has its separate model. How to provide the model to the nested view.
Here is the example.
my Index Controller:
public ActionResult Index()
{
ViewBag.CreateModel = new Todo();
return View(db.Todos.ToList());
}
my Index View:
IEnumerable<ToDoMVC.Models.Todo>
#RenderPage("~/Views/Todo/Create.cshtml",ViewBag.CreateModel)
my Create View:
#model ToDoMVC.Models.Todo
// does operations with this model
Now, if I run the program it gives me some model type mismatch error for create view.
So, how to solve this? How to provide another model to a nested view from a view?
In your model you would have a seperate model as a variable.
For example
class Todo {
public CreateModel create {get;set}
}
Then when you pass through to the second view you would do something like this:
#RenderPage("~/Views/Todo/Create.cshtml",Model.create)
That will pass that second model to your create page
Edit
Re-reading your question.
The mismatch will be because you are passing the whole model through and you want to pass an individual list item in.
So you need too loop through the model like this:
foreach(var item in Model)
{
#RenderPage("~/Views/Todo/Create.cshtml", item)
}
Simply create a view model class like this:
public class ToDoViewModel
{
public IEnumerable<ToDo> Todos{ get; set; }
public ToDo NewToDoItem { get; set; }
}
Then change your contoller's action:
public ActionResult Index()
{
var todoViewModel = new ToDoViewModel();
todoViewModel.NewToDoItem = new Todo();
todoViewModel.Todos= db.Todos.ToList();
return View(todoViewModel);
}
And then adjust your Index view:
#model ToDoMVC.Models.ToDoViewModel
#RenderPage("~/Views/Todo/Create.cshtml",Model.NewToDoItem)
A view inside a view or inother words, a nested view is not viable as far as my affair with MVC goes. You create a view and bind it to a model to show data relevant to that model on the browser. Now if you want data from other multiple models / business entities to be rendered on the view then you use ViewModels (Reference 1,Reference 2) and for that matter, the answers above remain valid. You can also customize / define sections on your layout to render specific information aswell. Partial views also make an option depending on the kind of info you want delivered to the view.
You can create a MasterModel and refer it to index view
public Class MasterModel
{
Public IEnumerable<ToDo> TodoList { get; set; }
Public ToDo CreateModel { get; set; }
}
Controller
public ActionResult Index()
{
MasterModel model = new MasterModel();
model.CreateModel = new Todo();
model.TodoList = db.Todos.ToList();
return View(model );
}
Refer this to Index model
#model MasterModel
#RenderPage("~/Views/Todo/Create.cshtml",Model.CreateModel)

mvc passing data between controller and view

I am developing an application. I have created a view and a controller. The view has a button, on the click of which I am supposed to do database operations. I have put the database operations in the model, I am creating the object of model in the controller. On clicking the button the action is handled by a method in the controller, and the object of the model is created to get the records from the database. I would like to know if there is any way to display this data in the view.Is the approach correct or the view is supposed to interact with model directly to get the data.
Following is the code in controller that gets invoked on the button click
public ActionResult getRecord()
{
DataModel f_DM = new DataModel();
DataTable f_DT = f_DM.getRecord();
return View();
}
DataModel is the model class with simply a method "getRecord".
Any help will be highly appreciated.
I would like to add that i am using vs2010 and mvc4
Regards
you should write the logic of retrieving data in your controller. Store all your data in view model and pass it to the view.
for eg.
Model
namespace Mvc4App.Models
{
public class Product
{
public string Name { get; set; }
}
public class ProductViewModel
{
public Product Product { get; set; }
public string SalesPerson { get; set; }
}
}
Controller
public class ProductController : Controller
{
public ActionResult Info()
{
ProductViewModel ProductViewModel = new ProductViewModel
{
Product = new Product { Name = "Toy" },
SalesPerson = "Homer Simpson"
};
return View(ProductViewModel);
}
}
View
#model Mvc4App.Models.ProductViewModel
#{ ViewBag.Title = "Info"; }
<h2>Product: #Model.Product.Name</h2>
<p>Sold by: #Model.SalesPerson</p>
This is the best known practice to pass data from controller to the view.
you may use other techniques also like,
1. ViewData
2. ViewBag
3. TempData
4. View Model Object
5. Strongly-typed View Model Object
Yes, it's possible, but actually now very logical way to to this.
Lets follow your way. You have some View were you have a button, that will trigger this action.
For ex:
public ActionResult Index()
{
return View();
}
Inside view you can have a Ajax link, that will trigget your getRecord method:
<div id="GetDataDiv"></div>
<div>
#Ajax.ActionLink("Get Record", "getRecord", "ControllerName", null, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "GetDataDiv" })
</div>
In the getRecord method you should have:
public ActionResult getRecord()
{
DataModel f_DM = new DataModel();
DataTable f_DT = f_DM.getRecord();
return PartialView(f_DT);
}
And in View it should be:
#model DataTable
#Model.PropertyOne #Model.PropertyTwo
It should works for you.
Actually same exaple here: http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=151

How to populate mvc razor partial view

I need to create a view that displays Order Header information and I need to add a patial view that displays a grid of Line Items. The partial view will be strongly typed from a viewmodel. I understand that I should use the html helper #Html.Partial("Path/view"). I have only used controllers up til now to open a view, populating the viewmodel before sending it to the view. Since the partial view is being called form the html helper, I would like to know is what would be the best way to populate the parital view with the model data.
Option 1: Inherit from parent page
By default, any partial view rendered by calling #Html.Partial("PartialViewName") will get the view model passed to the parent view.
So if you have:
View Model
namespace MyNamesapce
{
public OrderInfoViewModel
{
public string OrderTitle { get; set; }
public IEnumerable<OrderItem> OrderItems { get; set; }
}
}
OrderInfo.cshtml
#model MyNamespace.OrderInfoViewModel
<h1>#Model.OrderTitle</h1>
#Html.Partial("OrderLineItems")
The OrderLineItems page should get a MyNamespace.OrderViewModel passed to it... so your partial view should look like this:
OrderLineItems.cshtml
#model MyNamespace.OrderInfoViewModel
foreach (var orderItem in Model.OrderItems)
{
//Do stuff
}
Option 2: Specify model
You can use the second parameter to specify the view model to be passed. I.e.
OrderInfo.cshtml
#model MyNamespace.OrderInfoViewModel
<h1>#Model.OrderTitle</h1>
#Html.Partial("OrderLineItems", Model.OrderItems)
OrderLineItems.cshtml
#model IEnumerable<OrderItem>
foreach (var orderItem in Model)
{
//Do stuff
}
Option 3: Use partial actions
If you need to reuse a partial view over multiple pages, it could be a good idea to use a partial view to eliminate having to populate different view models with the same info just because the page is going to be using the same partial.
E.g.
View Model
namespace MyNamesapce
{
public OrderInfoViewModel
{
public string OrderTitle { get; set; }
}
}
Controller
public class OrderController : Controller
{
public ActionResult OrderInfo(int orderId)
{
OrderInfoViewModel viewModel = GetViewModel(orderId);
return View(viewModel);
}
public PartialViewResult OrderLineItems(int orderId)
{
IEnumerable<OrderItem> orderItems = GetOrderItems(orderId);
return Partial(orderItems);
}
}
OrderInfo.cshtml
#model MyNamespace.OrderInfoViewModel
<h1>#Model.OrderTitle</h1>
#Html.Action("OrderLineItems")
OrderLineItems.cshtml
#model IEnumerable<OrderItem>
foreach (var orderItem in Model.OrderItems)
{
//Do stuff
}
With a partial view, you are just sending in a Model just like you would with a normal View. For example, if your Model has a property of LineItem objects named 'LineItems' you simply would do this:
#Html.Partial("_PartialName", Model.LineItems)
Now if your Model does not have that property, you can either add it, or pass it another way, like ViewBag (I prefer a strongly typed method, but that is my opnion:
#Html.Partial("_PartialName", (List<LineItem>)ViewBag.LineItems)
These are not the only ways, but they are my preferred methods.

Best way to handle Categories Partial View in ASP.NET MVC 3 application

I have a list of blog post categories(~20) in a look up table.
I want to display them on multiple pages as list of hyperlinks that user can click.
I also want to display them in a dropdown list in 2 or more places(different view pages)
The follow works & I see categories as a menu/list of hyperlinks.
But this will cause me modify multiple controller where I need to show the categories.
What is the best practice to handle this so that I have minimal code change?
//#1 I added new class in one of my model:
namespace MyApp.Models
{
...
public class ShowPostModel
{
public Post Post { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
public class Category
{
public string _id { get; set; }
public string Name { get; set; }
}
}
//#2 Populating the controller
namespace MyApp.Controllers
{
public class BlogController : Controller
{
public ActionResult ShowPost()
{
ShowPostModel viewModel = new ShowPostModel();
viewModel.Post = ReadBlogPostFromDB();
viewModel.Categories = ReadCategoriesFromDB();
return View(viewModel);
}
}
}
//#3 This is from my main view for showing the Post:
#Html.Partial("_Categories", Model.Categories)
//#4 This is my _Categories partial view:
#model IEnumerable<MyApp.Models.Category>
<section>
<header><b>Categories</b></header>
<ul style="padding:0;margin:0;">
#foreach (var cat in Model)
{
<li>
#cat.Name
</li>
}
</ul>
</section>
Thanks for reading
Edit:
I made these changes and it seems working as well.
Any comments or improvements I can make here?
//#1 deleted this line from public class ShowPostModel (model is now DRY)
public IEnumerable<Category> Categories { get; set; }//deleted
//#2 created a base controller and inherit from it
public abstract class BlogBaseController : Controller
{
public BlogBaseController()
{
ViewBag.Categories = ReadCategoriesFromDB();
}
}
//#3 force all controller where I need categories to inherit from base controller
public class BlogController : BlogBaseController
//#4 change how I read in my views
#Html.Partial("_Categories", (IEnumerable<MyApp.Models.Category>)#ViewBag.Categories)
If you use the categories in enough places, you can encapsulate this into a base controller class, and override OnActionExecuted.
I would then put the Categories into a property on the ViewBag and pass it into your partial view from there, and leave your view's model alone.
i wonder why no one has suggested using RenderAction. you can write this Action method in you base controller. this will make it available in all derived controller. this way you can have your categories view strongly typed. Moreover, you should put your Categeories view in Views/Shared directory so every controller has access to this view. Doing so will keep you DRY and you still have the benefits of having strongly typed view.
EDIT By the way you don't have to have base controller to use renderaction. Although above approach is valid and i prefer doing like this but you can also have a nvaigation controller like
Public NavigationController:Controller()
{
public ActionResult Categories()
{
var Categories = FetchFromDB();
return View(Categoires);
}
}
Now you can call this action method using renderAction on anywhere in your application
You might want to try creating 2 display for templates, one to display in link and one to display in dropdown. Depending on the page you tell the view to use the specific template.
You can create a Filter that populates your categories and adds it to ViewData/ViewBag. You can then apply this filter to the controllers/actions that require the categories.
For displaying, you can use EditorTemplates or Partials to keep your UI code DRY...
HTH.

Resources