Passing (local variable)ListCourses and showing the list at the view MVC
You are passing courseinfo
which is List<Courses>
but you your view is expecting the a model of type Project.Models.Courses
.
If you still want to pass list of Courses then change the model in the view as below.
@model List<Project.Models.Courses>
Also, when you want to access the properties of Model then you need iterate the list using foreach as below.
@foreach(var course in Model)
{
<b>
<b>Course ID : @course.CourseID</b>
<b>Course ID : @course.Day</b>
<b>Course ID : @course.Hour</b>
</b>
}
passing variable from controller to partial view rails
For passing instance variables to partials you can use the locals
:
# view from
<%= render partial: 'skillsetdata', locals: { skills_set_data: @skillSetData } %>
While in the local you receive the value of that instance variable, which is now a local one:
# partial
<% skills_set_data.each do |single_skill| %>
<option value="<%= single_skill.skillname %>">
<%=single_skill.skillname %>
</option>
<% end %>
Accessing controller data in order to pass local variables for rendering a haml partial in a view
As long as the data is available in your outer view, you can pass it to a partial using the locals
option. For instance, if this was on your home page, you could pass the data to the controller like this...
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
@data = Model.get_data
end
end
Then, in your view, pass that @data
into your partial using the locals
option of the render
method:
# app/views/pages/home.html.erb
<%= render :partial => 'data_popups/events', :locals => { :data => @data } %>
Simple passing data from controller to view in c#
for a simple variable add it to the ViewBag and then reference it via @Model
for example
public ActionResult Index()
{
var Home = "No place like";
ViewBag.Home = Home;
return View();
}
In View:
@ViewBag.Home
will display what you've set.
As you want to add more complex data structures you will want to pass through a model object to the view.
You can pass this like
public ActionResult Index()
{
var myViewModel = new MyViewModel(); // set up view model with data
return View(myViewModel);
}
and then you can reference that from the view too!
{
@model = MyViewModel;
}
and refer to it later
<p>@model.PropertyName</p>
passing variable from Controller to View
The code have various issues:
render() save the new View in a local var so that don't exists after the function end
You can't expect
$this->showAddresses
to have a value at the constructor time.
You should implement the render() method as a method outside of the View constructor.
function __construct()
{
parent::__construct();
require 'view/head.php';
$result = $this->showAddresses; // (NULL) The object is not created yet
require 'view/foot.php';
}
View class:
public function factory($plik) // Old render($splik) method
{
return new $plik();
}
addressesView class:
function __construct()
{
parent::__construct();
}
function render()
{
require 'view/head.php';
$result = $this->showAddresses; // Object is created and the field has a value
require 'view/foot.php';
}
Controller class:
$view = $this->view->factory('addressesView');
$view->showAddresses = $this->model->getAddresses();
$view->render();
Is it possible to render a partial by passing a local variable for the collection?
Please try it
<%= render partial: 'item_kanban', collection: items, as: :item %>
Now you can access
<%= link_to item, class: "list-group-item" do %>
<%= item.number %>
<% end %>
When you did
<%= render partial: 'item_kanban', collection: items %>
will give you a item_kanban
local variable and not a item
variable.
So you need to specify it by as: :item
now you can access item
Views: is it better to set instance variables or pass in locals?
In Rails, it's best practice to pass the instance variables to your views. Rails developers are used to it, it's familiar, it's less typing, it works.
Yet, my personal preference is to pass locals
to render
because it makes your view variables more explicit. It also has a benefit of raising a "no method" exception if you mistype a variable rather than getting a "nil" exception when using instance vars.
I'm not sure there is going to be much difference in performance either way. Might be interesting running some benchmarks. But if you're using Rails, then view rendering speed is probably not your top concern.
Note that if you use an instance var in your controller to memoize a method's result (say to cache an expensive calculation), than that instance var "leaks" into your view. You probably didn't need it in your view, but you still get it (because Rails exposes any instance vars in controllers to your views). Thus, passing local vars is more explicit in communicating "my view relies on this variable")
Related Topics
Ruby Method, Proc, and Block Confusion
Checking If Activerecord Find Returns a Result
Equivalent Ruby .Times in Coffeescript
Rails Include Only Selected Columns from Relation
Standalone Ruby -- How to Load Different Environments from Database.Yml
How to Create Pretty JSON in Chef (Ruby)
"Ruby.Exe Is Not Recognized as an Internal or External Command" in Windows 7
Escaping the .Each { } Iteration Early in Ruby
How Do Enumerators Work in Ruby 1.9.1
When Installing Gems "Error: Permission Denied Rb_Sysopen" Arch Linux
How to Do Fuzzy Substring Matching in Ruby
Combine Thumbnails to One Large Image with Rmagick
Is There a Shorthand If (Without Else) Statement in Ruby on Rails
How to Use Ffmpeg on a Remote Machine via Ssh
What's the Easiest Way to Send a Message Through Outlook with Ruby
How to Refer a Local Gem in Ruby