Controller class
class EntidadController {
def index(){
def entidades = Entidad.list()
[entidades:entidades]
render(view:"index")
for (x in entidades)
{
print(x.nombreEntidad)
}
}
}
Domain class
class Entidad {
String nombreEntidad
int porcentaje
static hasOne = [kiosko: Kiosko]
static belongsTo = [adminCreador: Administrador,entidadSuperior: Entidad]
static hasMany = [adminEntidad: Administrador, entidadesInferiores: Entidad]
static constraints = {
kiosko nullable:true
nombreEntidad nullable : false
adminCreador nullable : true
adminEntidad nullable : true
entidadSuperior nullable : true
entidadesInferiores nullable : true
}
}
View in gsp
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<p>test</p>
<div controller="entidadController">
<g:each in="${entidades}" var="x">
<tr>
<td>${x.id}</td>
</tr>
</g:each>
</div>
</body>
</html>
I think code is fine but i dont know why is not showing anything at the g:each tag, ive tried with a static array in gsp page and g each worked and i print at console and the array "entidades " had objects on it, its like my view cant read data from controller
Perhaps a render statement like this:
render(view:'index', model:[entidades: entidades])
If you don't need pagination and sorting in your view, you can do like this on your gsp
...
<body>
<p>test</p>
<div controller="entidadController">
<g:each in="${Entidad.list()}" var="x">
<tr>
<td>${x.id}</td>
</tr>
</g:each>
</div>
</body>
...
The problem is here:
def index(){
...
[entidades:entidades]
render(view:"index")
...
}
You are not sending entidades to the view. This can be corrected this way (render docs):
def index(){
...
render(view:"index", model:[entidades:entidades])
...
}
Also, if your view and your action share the same name, you don't need to use render method explicitly, but the map as the last line of the action (this is a grails convention). Like this:
def index(){
...
[entidades:entidades]
}
Related
Here is my scenario:
Class Domain1 {
static hasMany=[ tests : Domain2 ]
static constraints = { tests(nullable: true) }
}
And
Class Domain2 {
Double t1, String t2
static constraints={
t1(nullable:true
t2(nullable:false,blank:false)
}
}
I need to display t1 from domain2 in domain1 with the ability to edit.
I need to display t1 from domain2 in domain1 with the ability to edit.
See the project at https://github.com/jeffbrown/samdomain.
grails-app/domain/samdomain/Domain1.groovy:
package samdomain
class Domain1 {
static hasMany = [tests: Domain2]
}
grails-app/domain/samdomain/Domain2.groovy:
package samdomain
class Domain2 {
Double t1
String t2
static constraints = {
t1 nullable: true
}
}
grails-app/controllers/samdomain/DemoController.groovy:
package samdomain
class DemoController {
def index() {
def d1 = new Domain1()
d1.addToTests t1: 42, t2: 'Fourty Two'
d1.addToTests t1: 2112, t2: 'Twenty One Twelve'
[domainInstance: d1]
}
def update() {
render "Updated values: ${params.t1Values}"
}
}
grails-app/views/demo/index.gsp:
<%# page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta name="layout" content="main"/>
<title>Simple Demo</title>
</head>
<body>
<g:form action="update" method="POST">
<table>
<g:each var="d2" in="${domainInstance.tests}">
<tr>
<td>${d2.t2}</td>
<td><g:textField name="t1Values" value="${d2.t1}"/></td>
</tr>
</g:each>
</table>
<g:submitButton name="Update"/>
</g:form>
</body>
</html>
Hi i have just started MVC 4 in c#, i am having issue in rendering partial view.
i have created model for the partial view and controller action in which i am just
sending a single string for the testing reasons but when i try to render it.
it just show the following error.
[NullReferenceException: Object reference not set to an instance of an object.]
here is my controller class.
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using my_photos.Models;
namespace my_photos.Controllers
{
public class DefaultController : Controller
{
public ActionResult Index()
{
ViewBag.Massage = "Hello Word";
return View();
}
public ActionResult About() {
ViewBag.Message = "About Page";
return View();
}
public ActionResult _RightView() {
var model = new my_photos.Models.partial(){
Winner = "Shafee Jan"
};
//ViewData["name"] = model;
return View(model);
}
}
here is model class for partial view
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace my_photos.Models
{
public class partial
{
public string Winner { get; set; }
}
}
here my partial view
#model my_photos.Models.partial
<div class="body">
<div class="content">
<div class="header">
<h1 class="nav-heading">Data</h1>
<p class="paragraph" > Winner :#Model.Winner.ToString() </p>
</div>
</div>
<div class="bottom">
<div class="header">
</div>
</div>
</div>
and here is my main layout in shared folder
#model my_photos.Models.partial
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>_Layout</title>
<link href="#Url.Content("~/Content/Style/Site.css")" rel="stylesheet" type="text/css" />
<link href="#Url.Content("~/Content/Style/Side_barnav.css")" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h3> Photos app</h3>
<p> In this app my major concerns are with the <strong>theaming</strong>!</p>
<div class="nav">
<ul class="main-menu">
<li>#Html.ActionLink("Home","Index", "Default")</li>
<li>#Html.ActionLink("About","About", "Default")</li>
<li>#Html.ActionLink("Contact","Contact", "Default")</li>
</ul>
</div>
</header>
<section>
#RenderBody()
#Html.Partial("~/Views/Shared/_partial.cshtml")
</section>
<footer>
<div class="fotter-menu">
<ul>
<li>#Html.ActionLink("Home","Index","Default")</li>
<li>#Html.ActionLink("About","About","Default")</li>
<li>#Html.ActionLink("Contact","Contact","Default")</li>
</ul>
<ul>
<li>#Html.ActionLink("Resources","Resources","Default")</li>
<li>#Html.ActionLink("Testimonial","Testimonial","Default")</li>
<li>#Html.ActionLink("Team","Team","Default")</li>
</ul>
<ul>
<li>#Html.ActionLink("Home","Index","Default")</li>
<li>#Html.ActionLink("Home","Index","Default")</li>
<li>#Html.ActionLink("Home","Index","Default")</li>
</ul>
</div>
</footer>
</body>
</html>
when ever i try to get value form the model it give me the following error.
kindly help me through this.
[NullReferenceException: Object reference not set to an instance of an object.]
This is happening because your model is null, as you can see, in actions About and Index there is no model being passed. Also partial is a keyword in C#, it will be better to have a more signifiant name.
To solve this you have to pass a model to every view that uses that layout that is expecting this model. In your case is null and an error is thrown.
public ActionResult Index()
{
ViewBag.Massage = "Hello Word";
var model = new my_photos.Models.partial(){
Winner = "Shafee Jan"
};
return View(model);
}
It is necesary to pass the model because on the below line of code the partial is rendered by default with the current view model.
#Html.Partial("_partial.cshtml", Model) #* Model is passed by default if there is no other parameter specified *#
I think what you really want to do is to call the _RightView action in your layout.
#Html.Action("_RightView", "Default")
Don't forget to modify the action to return a partial view and the passing of model in the other actions won't be necessary
public ActionResult _RightView() {
var model = new my_photos.Models.partial(){
Winner = "Shafee Jan"
};
//ViewData["name"] = model;
return PartialView("_partial", model);
}
hey this is not a big problem you are giving partial class reference to both layout and partial view.
so if you are access a _RightView Action it does't thrown a error because you are passing a object to view properly but when comes it partial view you are not passing object reference so just pass the model in
#Html.Partial("~/Views/Shared/_partial.cshtml",Model)
That's it
I have two domains declared in my app.
class Posts {
String title
String content
static hasMany = [tags:Tag]
static constraints = {
}
}
class Tag {
String Name
static belongsTo = Post
static hasMany = [posts:Post]
static constraints = {
}
String toString()
{
"Tag:${Name}"
}
}
I have a controller which manages the search and display of results:
package com.trafigura.com.trafigura
class ViewerController {
def defaultAction='search'
def search={}
def show = {
def _foundPost = Post.findAllBytitle(params.title)
return [posts: _foundPost, term: params.title]
}
}
The search.gsp code is as follows:
<html>
<head><title>Simple GSP page</title></head>
<body>Place your content here.
<formset>
<legend>TagsPosts</legend>
<g:form action="show">
<label for="title">Title</label>
<g:textField name="title" />
<g:submitButton name="search" value="Search"/>
</g:form>
</formset>
</body>
</html>
and the following code for show.gsp.
<html>
<head><title>Simple GSP page</title></head>
<body><h1>Results</h1>
for items matching <em>${term}</em>.
Found <strong>${posts.size()}</strong> hits.
</p>
<ul>
<g:each var="tag" in="${posts.tags}">
<li>${tag.Name}</li>
</g:each>
</ul>
<g:link action='search'>Search Again</g:link></body>
</html>
My Question is I am unable to display the tags as follows:
Results
Found 1 hits.
* [planting, dig]
However, I want the output as:
* planting
* dig
What am I doing wrong here?
Much Appreciated.
Replace
<g:each var="tag" in="${posts.tags}">
by
<g:each var="tag" in="${posts.tags[0]}">
I would like my views to be able to specify a class for the <body> tag, which lies in my master page.
My first take was to do something like this:
<asp:Content ContentPlaceHolderID="BodyClassContent" runat="server">
view-item</asp:Content>
However, that would require this in the master page, which doesn't work:
<body class="<asp:ContentPlaceHolder ID="BodyClassContent" runat="server" />">
Any solutions to this?
In the layout you can do this on the body tag:
<body #RenderSection("BodyAttributes", false)>
and then in your view you can do this:
#section BodyAttributes {
id="login" class="login"
}
Edit: I also had to do this working with VB.NET and WebForms today and found a handy link for achieving the equivalent
I would suggest a different approach.
You create an hierachy of view models, starting with the MasterModel. When you instantiate a view object, you pass a body class to it.
public class MasterModel
{
string BodyCss { get; set; }
public MasterModel (string bodyCss)
{
BodyCss = bodyCss;
}
}
public class MyView1Model : MasterModel
: base ("body-view1")
{
}
Then in your master view which should be strongly typed to MasterView:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MasterModel>" %>
you just write:
<body class="<%= Model.BodyCss %>"></body>
You could also specify the body id attribute in the View which wishes to set it:
#{
ViewBag.Title = "Test";
ViewData["BodyID"] = "test";
Layout = "~/Views/Shared/_Layout.cshtml";}
This helps to decouple the view from the controller, the controller (and/or view model) does not need to know about the id attribute of the body tag.
Set the id of the body tag in the master page like so:
<body id="#ViewData["BodyID"]">
Why don't you do in your masterpage:
<body class="<%=ViewData["bodyClass"].toString()%>">
and then set ViewData["bodyClass"] in your Controller actions? That should be equivalent...
My ViewModel class (ItemViewModel.cs) looks like this:
public class ItemViewModel
{
public ItemViewModel(xxx.Product product)
{
this.product = product;
}
private readonly xxx.xxx.Product product;
private readonly Pers pers;
private readonly Item item;
public xxx.xxx.Product Product
{
get{ return product;}
}
public Item Item
{
get { return item; }
}
public ItemList Items
{
get { return product.Items; }
}
public Pers Pers
{
get { return pers; }
set { value = pers; }
}
public PersList PersList
{
get { return product.PersList; }
}
}
The view has this code defined in it (I took out some other case lines, just to show one of them as an example):
<%# Page Language="C#" MasterPageFile="~/Views/Shared/MasterPages/Item.Master" Inherits="System.Web.Mvc.ViewPage<xxx.ViewModels.ItemViewModel>" %>
<% foreach (Pers p in Model.Perslist)
{
switch(p.DispType)
{
case DisType.Dropdown:
Model.Pers = p;
Html.RenderPartial("~/Views/Shared/Controls/Custom/PForm/DropDown.ascx",*Model);
break;
}
}
%>
And the RenderPartial looks like this:
<fieldset>
<div class="xxx">
<span class="xxx">*</span><label><%=Model.Pers.Name %></label>
<p class="xxx"><%=Model.Pers.Info %></p>
</div>
<div class="formField"><% Html.DropDownList(Model.Pers.Name, new SelectList(Model.Items[0].DropdownItems));%></div>
</fieldset>
The problem or dilemma I'm having is I not only need the p from the foreach but the entire ItemViewModel instance that was originally passed to my View. Because I need to use the Pers in that foreach as well as be able to reference the Items. So what I tried is to set the Pers property of the ItemViewModel class instance to the current p in the foreach. Then tried to send the whole Model (now that I have that Pers set on the property) which is of type ItemViewModel so that I can now use the property Pers on that object and also still be able to reference the Items property that was populated already when it hit the View.
So when the page renders I get:
System.NullReferenceException: Object reference not set to an instance of an object.
for this line:
<span class="xxx">*</span><label><%=Model.Pers.Name %></label>
So far I'm unsuccesful because I still get a null reference error on the property Pers when I attempt to use the ITemViewModel in my Partial View.
This could be because your partial view file needs this line above your html:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<xxx.ViewModels.**ItemViewModel**>" %>
So this is how your partial view should look:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<xxx.ViewModels.**ItemViewModel**>" %>
<fieldset>
<div class="xxx">
<span class="xxx">*</span><label><%=Model.Pers.Name %></label>
<p class="xxx"><%=Model.Pers.Info %></p>
</div>
<div class="formField"><% Html.DropDownList(Model.Pers.Name, new SelectList(Model.Items[0].DropdownItems));%></div>
</fieldset>
Good God, I set the property wrong. resolved. duh.