Is it possible to repeat content from one placeholder multiple times? - asp.net-mvc-2

Is it possible to use the content of a ContentPlaceHolder in multiple places in a master page?
For example, I often want to do something like:
<html>
<head>
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link rel="Stylesheet" href="../../Content/Corporate.css" />
</head>
<body>
<div id="header">
<h1>I'd like to reuse the TitleContent ContentPlaceHolder content here?</h1>
</div>
<div id="content">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
<div id="footer">
© 2012 Acme corp.
</div>
</body>
</html>

Answer is here, so please reward author, but to adjust it to MVC, here is what you need to do:
First: Define your own master page class (Site.Master):
<%# Master Language="C#" Inherits="MyProject.Views.Shared.MyMasterPage" %>
Second: Class content:
public class MyMasterPage : ViewMasterPage
{
protected override void RenderChildren(HtmlTextWriter writer)
{
var mainPlaceHolder = FindControl("TitleContent");
var doublePlaceHolder = FindControl("TitleContentDuplicated");
var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
using (var tw = new HtmlTextWriter(sw))
{
mainPlaceHolder.RenderControl(tw);
}
var lc = new LiteralControl(sb.ToString());
doublePlaceHolder.Controls.Add(lc);
base.RenderChildren(writer);
}
}
Third: Then you can use:
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:ContentPlaceHolder ID="TitleContentDuplicated" runat="server" />
</title>

Related

Show method from resource controller returns null

I'm having trouble with learning laravel and decided to ask my question here since I can't find an answer via Google etc.
I am trying to return a client from the database via Id, maybe later via name.
This is my form:
<<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Klant invoeren</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
</head>
<body>
<p><<form action="{{route('client/'.$client->id.'/show/')}}"
method="get">
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="price">Achternaam:</label>
<input type="text" class="form-control" name="id">
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
{{csrf_field()}}
<button type="submit" class="btn btn-success" style="margin-
left:38px">Zoek klant</button>
</div>
</div>
</form></p>
</body>
</html>
Which redirects to:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Klant invoeren</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
</head>
<body>
<div class="container">
<h2>Voer een klant in</h2><br />
<h1>Showing {{ var_dump($client) }}</h1>
</div>
</body>
</html>`
And this is the show method from the Clientcontroller:
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
This is the route I'm using:
Route::get('client/{id}/show','ClientController#show');
Can someone spot my mistake, because I can't after messing with this the last few hours.
EDIT: updated code and added the route, now getting the error that the client variable isn't defined in the
You must echo the client id in the form action. So, remove the inverted comma from $client->id. Use the following code :
<form action="{{action('ClientController#show', $client->id)}}"
method="get">
Additionally, remove backtick from the code. Use the following code :
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
In your routes/web.php
Route::get('client/{id}/show','ClientController#show');
In your form:
<form action="{{URL::to('client/'.$client->id.'/show/')}}"
method="get">
In your controller:
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
In your Client Controller make sure you use the Model that you are referencing to
Assuming App\Client.php Change it to App\User.php if you are referring to user
ClientController
use App\Client;
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
Now you can fetch the client model with $client For example $client->id
Make sure you pass the {id} parameter in the route

jsp form selection not working

I have following jsp page, when i select MYAP_FORM i expect the hidden form to be displayed but it is not.
Right now i have only one form with display none, i will be adding more and based on the selection will display right one.
Any inputs why i dont see the corresponding form displayed ...
Thanks
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page import="java.io.*"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="url">${pageContext.request.requestURL}</c:set>
<c:set var="baseURL" value="${fn:replace(url, pageContext.request.requestURI, pageContext.request.contextPath)}" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="${baseURL}/bootstrap/css/bootstrap.css">
<title>Testing</title>
</head>
<body>
<script src="${baseURL}/bootstrap/js/bootstrap.js"></script>
<form id="platform" class="container text-center">
<select id="myselect">
<option value="" selected="selected"></option>
<option value="MYAP_FORM">MYAP_FORM</option>
<option value="XYZ_FORM">XYZ_FORM</option>
</select>
</form>
<%
String file = application.getRealPath("/") + "xzyFiles.txt";
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String tmp;
out.println("<form class=\"container\" id=\"MYAP_FORM\" style=\"display:none\">");
while ((tmp = br.readLine()) != null) {
out.print("<label class=\"checkbox text-left\">");
out.print("<input type=\"checkbox\" id=\"" + tmp +"\" value=\"1\">" + tmp + "</BR>");
out.print("</label>");
}
fis.close();
out.println("<button class=\"btn btn-primary\">Submit</button>");
out.println("<button class=\"btn btn-primary\">Clear</button>");
out.println("</form>");
%>
<script>
$("#myselect").on("change", function() {
$("#" + $(this).val()).show();
})
</script>
</body>
</html>
JQuery Script was missing, once added it worked fine.

form submit button not working when view is using master layout

Following my previous question, now things are not working again, here it goes:
I have form in MVC 4, and the form button does not respond:
Controller (that should get the post action):
public class GeneralController {
[HttpPost]
public ActionResult SearchResults(SearchParamsModel searchParams)
{
// doin some stuff here
return View("SearchResultsView");
}
}
View (.cshtml):
#model MVC.Models.SearchParamsModel
#{
ViewBag.Title = "SomeTitle";
}
#Html.Partial("~/Views/SomePartials/ssi-header.cshtml");
#Html.Partial("~/Views/SomePartials/ssi-sidebar-notifications.cshtml");
<!-- content -->
<section class="content right">
<section class="some-search">
<div class="form-separator gradient"></div>
#using (Html.BeginForm("SearchResults", "Home", FormMethod.Post))
{
<section class="form-field">
<input type="text" name="City" id="City" class="field field139 autocomplete-init-no-img" />
<label for="City">City</label>
</section>
<input type="submit" value="Submit Search" class="submit btn blue-btn special-submit" />
}
</section>
</section>
<!-- end content -->
#Html.Partial("~/Views/SomePartials/ssi-footer.cshtml");
Model :
public class SearchParamsModel
{
public string City{ get; set; }
}
Things are only working if I add in View : Layout = null (!!!Wierd)
My Layout
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>#ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
#Styles.Render("~/Content/SomeStyles")
#*#Scripts.Render("~/bundles/SomeScripts")*#
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="../../Scripts/script1.js" type="text/javascript"></script>
<script src="../../Scripts/script2.js"></script>
....
</head>
<body>
<div id="body">
#RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
#RenderBody()
</section>
</div>
#*#RenderSection("scripts", required: false)*#
</body>
</html>
Like the answer in your previous question you should post to General controller and not Home controller.
#using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
Cheers.

Knockoutjs program is not working

I am new to Knockoutjs, starting with http://learn.knockoutjs.com/ tutorial but the same when i am trying in local it is not working.
Do i need to write model and view in separate file or same, My question is how to run first knockout program.
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script type="text/javascript" src='http://cdnjs.cloudflare.com/ajax/libs/knockout/2.1.0/knockout-min.js'></script>
<script type="text/javascript">
// Here's my data model
var ViewModel = function(first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.computed(function() {
// Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work​​​​​​​​​​​​​​​​​​
</script>
</head>
<body>
<div class='liveExample'>
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<h2>Hello, <span data-bind='text: fullName'> </span></h2>
</div>​
</body>
</html>
You should put ko.applyBindings to the end of page. Here is a quote from official documentation:
To activate Knockout, add the following line to a block:
ko.applyBindings(myViewModel);
You can either put the script block at the bottom of your HTML
document, or you can put it at the top and wrap the contents in a
DOM-ready handler such as jQuery’s $ function.
So code should look as follow:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script type="text/javascript" src='http://cdnjs.cloudflare.com/ajax/libs/knockout/2.1.0/knockout-min.js'></script>
</head>
<body>
<div class='liveExample'>
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<h2>Hello, <span data-bind='text: fullName'> </span></h2>
</div>​
<script type="text/javascript">
// Here's my data model
var ViewModel = function(first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.computed(function() {
// Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work​​​​​​​​​​​​​​​​​​
</script>
</body>
</html>
Here is working fiddle: http://jsfiddle.net/vyshniakov/s2LSE/

Button OnClick="btnSave_Click" does not work in MVC application

I am new to MVC architecture and I am struggling to get the Button onclick. Please help me trace the issue. I have a Telerik TreeView control, which is populated. Some of the nodes have child nodes and some of them doesn't.
Please let me know what changes need to be made to fix the issue
Here I am struggling to get it working.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/PostLogin.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<applications>>" %>
<%# Import Namespace="ApplicationGrps" %>
<%# Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<script runat="server" type="text/javascript">
protected void Page_Load()
{
ApplicationGrps.Visible = true;
ApplicationGrps.DataSource = ODS1;
ApplicationGrps.DataFieldID = "ApplicationName";
ApplicationGrps.DataFieldParentID = "ParentID";
ApplicationGrps.DataBind();
BindCheckedTags(ApplicationGrps);
}
protected void BindCheckedTags(RadTreeView treeView)
{
// Displays the checked nodes
}
protected void ODS2_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["ClientID"] = ViewData["ClientID"];
}
protected void btnSave_Click(object sender, EventArgs e)
{
//Iterate through the tree and get all the nodes that are checked and submit it.
}
</script>
<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">
<link href="<%: Url.Content("~/Content/script-libraries/jquery-ui-1.7.2.custom/css/smoothness/jquery-ui-1.7.2.custom.css") %>"
rel="stylesheet" type="text/css" />
<script src="<%: Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server" method="post">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<telerik:RadTreeView id="AppModulesTreeView" runat="server" CheckBoxes="True" Visible="true" >
</telerik:RadTreeView>
<asp:ObjectDataSource ID="ODS1" runat="server"
SelectMethod="GetAllApplicationModules"
TypeName="ELS.BOS.Services.EntitlementProxy.EntitlementServiceClient">
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ODS2" runat="server"
SelectMethod="GetApplicationModMappedByClientID"
TypeName="ELS.BOS.Services.EntitlementProxy.EntitlementServiceClient"
onselecting="ODS2_Selecting">
<SelectParameters>
<asp:Parameter Name="ClientID" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<%-- <telerik:RadButton runat="server" onclick="btnSave_Click"
Text="Save">
</telerik:RadButton>
<telerik:RadButton ID="btnCancel" runat="server" Text="Cancel"
>
</telerik:RadButton>--%>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnSave_Click" />
</div>
</form>
</asp:Content>
Please let me know if I need to use javascript or jquery to get this working. Please show me some samples on how to fire btnSave_click on button control click.
Thanks!
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnSave_Click" />, asp:ObjectDataSource, ScriptManager, ... in an ASP.NET MVC application?
No.
In an ASP.NET MVC applications you don't use server side controls (no runat="server"). You don't PostBack. There is no ViewState. There is no code behind. There are no events. There are no script managers. There are no object data sources. All this is specific to WebForms and is no longer used in MVC.
In an ASP.NET MVC application you have a Controller, a Model and a View. The view contains standard HTML markup which could be generated with the aid of HTML helpers. To call the controller you could use a normal link:
<%= Html.ActionLink("link text", "actionName", "controllerName") %>
The controller contains actions which manipulate the model and choose the view to be rendered by passing it all the information it needs to show.
I would strongly recommend you start reading the tutorials here: http://asp.net/mvc about how MVC works and familiarize yourself with the very basics of this framework.