How to divide a Certain Model into two parts in Razor View? - asp.net-mvc

I am using Articulate plugin in Umbraco for Blog Style and I need to show something default inside every Articulate Post.
Here is what is happening now.
<section class="post-content">
#Model.Body
</section>
And I need to do something below
<section class="post-content">
#Model.Body.part1
"Something Very Important"
#Model.Body.part2
</section>
Thanks in advance.

Create two partial views and pass the same model to both. On each view just render the part you want.
<section class="post-content">
#* the model will be passed to the partial view *#
#Html.Partial("~/Views/Partial/PartialView1.cshtml");
<p>Something very important here</p>
#Html.Partial("~/Views/Partial/PartialView2.cshtml");
</section>
Then your partial views would be something like:
PartialView1.cshtml
<div>
#Model.Body.part1
</div>
PartialView2.cshtml
<div>
#Model.Body.part2
</div>
You don't really need the <div> but you get the point, right?

Related

Grails formRemote updating wrong div

Having a comment within a comment but I'm having trouble in updating via form remote in correct place
I have this code
<div id="comment">
<g:render template="comment" var="comment" collection="${review.comment}" />
</div>
<g:formRemote class="ui comment form" name="commentForm"
url="[controller: 'game', action: 'addComment']" update="comment">
The problem is it's updating correctly in the database. but In the view it is only updating in the top most parent comment and not the correct comment
Sample pictures:
After clicking Add comment button :
After refreshing the page:
Don't mind the miss arrangement on the last picture I have a little sorting problem which I'm gonna fix later, the problem is the formremote updating the wrong one. The last picture is just to show that the update in the database is correct
edit:
Here is the action and template
def addComment(){
gameService.addComment(params.comment, params.gameId, params.userId, params.reviewId)
def comment = gameService.listComment(params.gameId,params.reviewId)
render(template: 'comment', collection: comment, var: 'comment')
}
template:
<div class="comment">
<a class="avatar"> <img
src="${createLink(controller:'user', action:'avatar_image', id:"${comment.user.id}" )}" />
</a>
<div class="content">
<g:link class="author" controller="user" action="userProfile"
params="${[userId:"${comment.user.id}"]}">
${comment.user.name }
</g:link>
<div class="metadata">
<span class="date"> ${comment.date }
</span>
</div>
<div class="text">
${comment.comment }
</div>
</div>
Looks to me like your first piece of code already is used multiple times, resulting in more than one div with the id "comment" (I guess that because you have multiple textareas and thus multiple forms in your screenshots, and this would totally explain your problem).
So, if you have multiple div-tags with the same id, the formRemote places the returned html inside the first one.
Edit: damn, just noticed that this question is quite old :-/

MVC - Best way to add outer element

I have been developing with MVC for a few years and this is a nagging issue I have encountered several times. I do not like any of the ways I have handled this in the past so I thought I would ask here.
Let's say I have a series of nested DIVs on my view:
<div id="outer">
<div id="inner1">
<div id="inner2">
</div>
</div>
</div>
At runtime I want to add another element, a DIV or anchor, inside of the outer div but have it contain the inner DIVs.
<div id="outer">
<div id="newone">
<div id="inner1">
<div id="inner2">
</div>
</div>
</div>
</div>
How would you recommend handling this?
I imagine this would have more to do with JavaScript than with the server-side code. And since ASP.NET MVC comes with jQuery, you may as well make use of the wrap() function. Something like this:
$('#inner1').wrap('<div id="newone"></div>');

load partial model after ajax - mvc

I have page containig partial views
<div class="window">
<div id="progress" class="progress">
</div>
#*<div id="tlv-dialog-content" >*#
<form id="___iskadetails">
<div id="iska-general-details">
#Html.Partial("_workGeneralDetails_EditForm", Model)
</div>
</form>
<div id="tabstrip" class="tabstrip">
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ul>
</div>
each partial in the tabstrip contain different model
i want to load the partial view after spacific ajax im doing in the client side
in the document.ready
what is the best way to do it
Use the JQUERY UI tab and load different Partial view with their model.
pls look following links that will help you.
http://kevgriffin.com/using-jquery-tabs-and-asp-net-mvc-partial-views-for-ajax-goodness/
http://ericdotnet.wordpress.com/2009/03/17/jquery-ui-tabs-and-aspnet-mvc/

Split ruby on rails view correctly

playing around with rails and got a little problem with the layout.
I have a simple home mvc.
Content of the home view is just
<h3>Home</h3>
<p>content</p>
I have my application view for overall design with some partials and so on.
<section>
<header>
<div class="pull-right">
<a class="btn btn-small">Edit</a>
<a class="btn btn-small">Blurm</a>
</div>
<h3>Head goes here</h3>
</header>
<%= yield %>
</section>
Now I come to my main Part for displaying the different pages with yield.
How should i split up the template? Should I put the complete application part to the home view to display the Heading in the right place? Or is there a possibilty to get the Heading different from the yield?
Any Suggestions?
P.S.: If someone have a nice tutorial or website for explaining How to structure and plan the views. A comment below would be nice.
best regards
dennym
I think that you are asking about using named yields.
From your structure, we add a yield named header
<section>
<header>
<div class="pull-right">
<a class="btn btn-small">Edit</a>
<a class="btn btn-small">Blurm</a>
</div>
<h3><%= yield :header %></h3>
</header>
<%= yield %>
</section>
And then we set the content for that named yield:
<% content_for :header do %>
My header
<% end %>
<p> Rest of page ...</p>
If you are just trying to change you header periodically I would suggest either you have different layouts that have different headers which you could specify in your controller by
layout :layout_name, or dynamically change header content using js.

Is there a way to do a rails "yield/content_for" in GRAILS?

In rails, I'm used to use the yield/content_for to site mesh. (http://guides.rubyonrails.org/layouts_and_rendering.html#using-content_for)
I can't find in Grails documentation a way to do this... Could you help me?
EDIT
Here is the situation:
I have a layout containing this:
<body>
<div id="wrapper">
<div id="container">
<div id="header">
<g:render template="/layouts/header"/>
</div>
<g:render template="/layouts/menu"/>
<div id="container-homepage">
<g:layoutBody/>
<div id="subfooter">
<div id="content">
<div id="left">
<div id="logo_sub_header"></div>
</div>
<div id="right"></div>
</div>
</div>
</div>
</div>
</div>
</body>
And I want to be able to add a html snippet (search tool bar for example) juste above the container-homepage div. A partial could do the trick.. if this search tool bar was always the same. The thing here is that this search bar depends on the page i'm visiting.
I could also just change the position of the container-homepage div to put it directly into the view, and not the layout, but then i'll have to to it in ALL the views, and that's not DRY.
Any ideas?
Regards,
I think you have two solutions:
the g:render tag is the best option if your content block will not change based on a custom page.
Anyway I would take a look ah this link
http://grails.org/Content+Blocks
because g:pageProperty it is the most elegant and flexible solution.
Perhaps the g:render tag is what you're looking for? It allows you to render a template anywhere within your view (including in other templates). You can also pass in a model for the template to use.
Note the section at the bottom of that page on naming conventions -- template view gsp filenames should begin with an underscore (though that underscore is not supplied in the render tag). There's mos def a way to override this, but things work automagically if you put the underscore there.

Resources