Call java void function without printing any text in the page source - thymeleaf

Here is my sample thymeleaf code:
<script th:inline="javascript">
/*<![CDATA[*/
/*[[${model.setPRINTOUTFLG("0")}]]*/
var flgPrintOut2 = /*[[${model.getPRINTOUTFLG()}]]*/ null;
/*]]>*/
</script>
In the source, the result is like this:
I just want to call the void function without printing any text. How can I do that using Thymeleaf?
I also tried this:
<script th:inline="javascript">
/*<![CDATA[*/
/*[[${model.setPRINTOUTFLG("0") == null ? "" : ""}]]*/
var flgPrintOut2 = /*[[${model.getPRINTOUTFLG()}]]*/ null;
/*]]>*/
</script>
But the result becomes like this:

Related

Knockout Mapping attempt not mapping with JSON responses

I am new to knockout and cannot seem to get a simple mapping to work at all. I am in an MVC4 C#.
My View looks like such:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Index";
}
<h2>Simple Mapping Example</h2>
The time on the server is: <span data-bind="text: ServerTime"></span>
and <span data-bind="text: NumberOfUsersConnected"></span> user(s) are connected.
#section Scripts
{
<script type="text/javascript">
function ViewModel() {
var self = this;
self.ServerTime = ko.observable();
self.NumberOfUsersConnected = ko.observable();
}
var vm = new ViewModel();
ko.applyBindings(vm);
$.getJSON('/Mapping/GetServerInfo', function(data) {
ko.mapping.fromJSON(data, {}, vm);
});
</script>
}
Example JSON that is returned from the controller call 'Mapping/GetServerInfo' is:
{"ServerTime":"2013-03-13T14:24:10.5675104-07:00","NumberOfUsersConnected":5}
For the data-binds I have tried text and value, neither one causes any data to be bound and displayed.
Am I missing something really obvious?
As per the question in the comments yes this is what is in my Layout.
<script type="text/javascript" src="~/Scripts/knockout-2.2.1.js"></script>
<script type="text/javascript" src="~/Scripts/knockout.mapping-latest.js"></script>
Thanks
From the $.getJSON documentation
The success callback is passed the returned data, which is typically a
JavaScript object or array as defined by the JSON structure and parsed
using the $.parseJSON() method.
So your the data variable is holding an already parsed object so you need to use the ko.mapping.fromJS method:
$.getJSON('/Mapping/GetServerInfo', function(data) {
ko.mapping.fromJS(data, {}, vm);
});

How to use Knockout.js in asp.net mvc Razor correctly and sufficiently?

i try to learn knockout.js on asp.net mvc razor. i have been coding below code to learn and test myself
But View side throws me a js error.
Error occurs on "var model = #Html.Raw(Json.Encode(Model));" Error : Microsoft JScript run-time error: 'fromJSON' Unable to get value of the property: the object is empty or undefined
Controllers:
[HttpGet]
public ActionResult GetGift()
{
GiftModel gift = new GiftModel();
gift.Price = 120;
gift.Title = "Test";
return View(gift);
}
View:
#using System.Web.Script.Serialization;
#model knockout1.Models.GiftModel
#{
ViewBag.Title = "GetGift";
}
<h2>GetGift</h2>
<script src="/Scripts/knockout-2.1.0.js" type="text/javascript"></script>
<script type="text/javascript">
var initialData = #Html.Raw( new JavaScriptSerializer().Serialize(Model));
var viewModel = ko.mapping.fromJSON(initialData);
$(document).ready(function () { ko.applyBindings(viewModel); });
</script>
<p>Title: <strong data-bind="text: Title"></strong></p>
<p>Price: <strong data-bind="text: Price"></strong></p>
But i changed my js codes. Error disappears. i can not understand first usage why doesn't correct? i readed Darin Dimitrov's response:
Darin Dimitrov :
<script type="text/javascript">
var jsonResultData = #Html.Raw(Json.Encode(Model));
</script>
Me: (it is working good.)
<script type="text/javascript">
$(function()
{
var model = #Html.Raw(Json.Encode(Model));
// Activates knockout.js
ko.applyBindings(model);
});
</script>
Based on the error message
'fromJSON' Unable to get value of the property: the object is empty or undefined
and page setup your problem is that you are trying to use the KO mapping plugin without including the plugin.
All the methods which are string with ko.mapping are part of the mapping plugin and for using them you need to refeence the knockout.mapping.js file in your HTML page.
Can you can download mapping plugin from github

WebView.InvokeScript always returns empty string

I've got a problem with WebView control. I'm developing Windows 8 Metro Style app and I'm trying to invoke script in content of WebView, but it's always returns empty string.
I've tried to make it "WebView.InvokeScript("eval", new string[] { "document.getElementById('contentDiv').offsetHeight" }) as well but it works the same - empty string. I really have no guesses what wrong is here.
public void Sethtml(string html) {
var toSet = string.Format(Style, html);
wv.LoadCompleted += wv_LoadCompleted;
wv.NavigateToString(toSet);
}
void wv_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
WebView wv = sender as WebView;
string height = wv.InvokeScript("heightFun", new string[0]);
}
public static readonly string Style = #"<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
body {{
font-family: Segoe UI;
}}
</style>
<script type='text/javascript'>
var heightFun = function() {{
return document.getElementById('contentDiv').offsetHeight;
}}
</script>
</head>
<body>
<div id='contentDiv'>
{0}
</div>
</body>";
You must use the window.external.notify to send a value back.
See the forum post:
http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/ac390a6a-d4ad-408c-8b13-93f9694cbabe
Just solved this problem via a hint of the previous answer.
Make sure your return value is a string.

How to display alert message after inserting data into database?

I have create web app in MVS 3
but failed to display alert message after data insert into database
Controller code:
[HttpPost]
public ActionResult QuestionBank(QuestionBank questionbank)
{
if (ModelState.IsValid)
{
dbEntities.QuestionBanks.AddObject(questionbank);
dbEntities.SaveChanges();
//questionbank.SendEnquiryEmail(questionbank);
ViewData["Message"] = "Data inserted";
return RedirectToAction("QuestionBank");
}
return View(questionbank);
}
Used ViewData["Message"] = "Data inserted"; which is not displayed message :(
whats going wrong or i placed it somewhere else?
OR ELSE I MAY HAVE THIS CODE
<script type="text/javascript">
//i'm using jquery ready event which will call the javascript chunk after the page has completed loading
$(document).ready(function () {
//assuming that your variable name from the code behind is bInsertSuccess
var bSuccess = "<%= myvar %>";
if (bSuccess) {
alert("Successfully Inserted");
}
});
</script>
but i dont know where i declare that variable myvar which checks insertion plz help
On your .chsthml page:
<script type="text/javascript">
$(document).ready(function () {
var msg = '#ViewBag.Message';
alert(msg);
});
</script>
in your action:
ViewBag.Message = "1";
Edit: Apply conditional check in script:
<script type="text/javascript">
$(document).ready(function () {
var msg = '#ViewData["Message"]';
if(msg=='1')
alert("you are done with your thing");
});
</script>
In view:
ViewData["Message"] = "1";
ViewData["Message"] would result in same thing here.
Try to user
TempData
instead of
ViewData

Add on-click event code to a LabelField

In my application there is a LabelField with text "www.google.com" When the user clicks, the default browser should open to www.google.com.
try this code
final LabelField label = new LabelField("http://www.google.com",LabelField.FOCUSABLE){
public boolean navigationClick (int status , int time){
BrowserSession bSession = Browser.getDefaultSession();
bSession.displayPage(label.getText());
return true;
}
};
you may use Jquery.
try this code:
<html>
<head>
<title>Opens a link</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
<label for="link" id="target">www.google.com</label>
<script type="text/javascript">
$('#target').click(function () {
var url = $(this).text();
window.location = "http\://"+url;
});
</script>
</body>
</html>

Resources