How can I avoid repeating handlebar templates in cshtml on MVC project? - asp.net-mvc

I have handlebars and I want them to be rendered conditionally.
F.e pseudo code:
if(condition)
{
<script id="template-1" type="text/x-handlebars-template">
...................
</script>
<script id="template-2" type="text/x-handlebars-template">
...............
</script>
}
else
{
if(condition2)
{
<script id="template-1" type="text/x-handlebars-template">
................
</script>
}
else
{
<script id="template-2" type="text/x-handlebars-template">
..........
</script>
}
}
so I do not want to repeat template1 and template2 handlebars in cshtml. How can I achieve this?

Related

How to load script to dynamic model

I'm trying to add a script to my view head part. Problem is when I load view I'm getting a console error "Uncaught ReferenceError: myfunction is not defined". As I understand my script wasn't loaded. I just started working with .NET.ASP MVC.
Here is part of my view.cshtml
#model dynamic
#{
var rUser = ViewBag.User as MyAplication.Model.User;
}
#section head {
#if (!rUser.IsPremiumMember)
{
<script type="text/javascript">
myfunction
</script>
<script type="text/javascript" async="async" data-cfasync="false" src="https://script.js"></script>
<script type="text/javascript" async="async" data-cfasync="false" src="https://otherscript.js"></script>
}
}

Autocomplete textbox in MVC returning '[ ]' when values populated from databse and does not show the view with the textbox control placed in

I used the following code to implement autocomplete textbox in MVC. But when I debug the project it shows a pair of angle brackets in the browser. pls examine my code.
controller:
public ActionResult Autocomplete(string searchstr)
{
var tenders = db.farmas.Where(t => t.Itemname.ToLower()
.Contains(searchstr.ToLower())).Take(10)
.OrderBy(t => t.Itemname)
.Select(t => new { label = t.Itemname });
return Json(tenders, JsonRequestBehavior.AllowGet);
}
view:
<!DOCTYPE html>
<html>
<head>
<h2>Autocomplete</h2>
<link href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css rel="stylesheet"
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js">
</script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.js">
</script>
#Scripts.Render("~/bundles/javascript")
#Scripts.Render("~/bundles/jqueryval")
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryui")
#Styles.Render("~/Content/themes/base/css")
<script type="text/javascript">
$(document).ready(function () {
$('#Itemname').autocomplete({
source: function (request, response) {
$.getJSON("/NewDefault1/Autocomplete?term=" + request.term, function (data) {
response(data);
});
},
minLength: 3,
delay: 100
});
});
</script>
</head>
<body>
<div id="main">
<div style="padding: 20px;">
pls type in the textbox
</div>
<div style="padding: 20px;">
Product: #Html.TextBox("Itemname")
</div>
</div>
<div id="footer">
</div>
</div>
</body>
</html>
before I had this error that downloaded the file with JSON extension.when the project is debug,which contained the same square brackets as its contents and now the same is shown on the browser . what is wrong in the code? where am I wrong? I also went through the jquery-ui website,but i am not able to make out how to populate the textbox values from database. any help will be appreciated. Thanks in advance.

How to set button as default selected class (jquery selectable)

Hey there, total javascript noob here. Just wondering, I've set up some buttons inside ul tags using jquery selectable, which change colour when I click on them. One problem though, how do I make one of them select on the load of the page?
Thanks!
edit: here's my code:
<link rel="stylesheet" href="style/style.css" media="screen,projection" type="text/css" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
setLink('single');
$('#target').load($('#single a:first').attr('href'));
});
function setLink(id){
$('#'+id+' a').click(function(e){
$('#target').load(
this.href,
function(){
setLink('target');
}
);
return false;
});
}
</script>
<style type="text/css">
#selectable .ui-selecting {
background-image:url(../images/nav-li-bcg.png);
}
#selectable .ui-selected {
background-image:url(../images/nav-li-bcg.png);
}
</style>
<script>
$(document).ready(function() {
$("#selectable").selectable();
$document.getElementById('current1').click();
});
</script>
</head>
<body>
<div id="tabs">
<div id="navigation">
<div id="single">
<div class="anchor1">
<ul id="selectable">
<li class="current1" id="current1"><span>1</span></li>
<li><span>2</span></li>
<li><span>3</span></li></ul>
</div>
<div class="clear"></div></div></div><!-- navigation -->
<div class="info">
<div class="top"></div>
<div id="target">
</div>
Simulate a click on document ready?
$(document).ready(function() {
$(".selectable .buttonSelector:first").click();
});
$('#myListItem').addClass('ui-selected');

Cannot use click action in jquerymobile

I tried add click function on item, but seems that does not work correctly.
$(document).ready(function(){
$("#vibrateBtn").click(function() {
window.alert('test');
});
});
I tried also disable Ajax loading, with>
$(document).bind("mobileinit", function(){
ajaxEnabled:false;
});
But with same result.
Where can be problem please?
Thanks for any advice.
in jquery mobile document.ready() doesn't work and use .live to bind the click event instead of using the short hard .click().
There are other event like pagebeforecreate, pagecreate. check the jquery demo for the list of event.
$(document).live('pageshow',function(){
$("#vibrateBtn").live('click',function() {
window.alert('test');
});
});
Please try this code
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script language="javascript">
$(function() {
$('#theButton').on('tap click',function(event, ui) {
alert('The Button has been clicked');
});
});
</script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<input type="button" id="theButton" name="theButton" value="The Button">
</div>
</div>
</body>
</html>

Sys.ArgumentUndefinedException: Value cannot be undefined

I am developing some ajax stuff on asp.net mvc framework beta.
but,I got exception as following.
Anyone has problem like me?
Sys.ArgumentUndefinedException: Value cannot be undefined.
and my source code is like this.
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script type="text/javascript">
var myView;
$(pageLoad);
function pageLoad() {
myView = $create(Sys.UI.DataView, {}, {}, {}, $get("ajaxResult"));
$("#callAjaxButton").click(callActionMethod);
}
function callActionMethod() {
$.getJSON("/Home/GetCategories", bindData);
}
function bindData(data) {
myView.set_data(data);
}
</script>
<input type="button" id="callAjaxButton" value="ajaxCall" />
<div id="ajaxResult"></div>
</asp:Content>
From the snippet you provided there are a couple of things to consider:
You are missing a script reference to the Microsoft ASP.NET 2.0 AJAX Templates for Visual Studio 2008.
You are using the jquery's document.ready function (raised whenever the DOM is ready to be traversed and manipulated) instead of the System.Application.init event (raised after all scripts have been loaded but before objects are created).
Can you try this to see if it works for you:
<script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjaxTemplates.debug.js" type="text/javascript"></script>
<script type="text/javascript">
var myView;
Sys.Application.add_init(pageLoad);
function pageLoad() {
myView = $create(Sys.UI.DataView, {}, {}, {}, $get("ajaxResult"));
$("#callAjaxButton").click(callActionMethod);
}
function callActionMethod() {
$.getJSON("/Home/GetCategories", bindData);
}
function bindData(data) {
myView.set_data(data);
}
</script>
<input type="button" id="callAjaxButton" value="ajaxCall" />
<div id="ajaxResult"></div>
Scott Hanselman has written a nice post on this subject.

Resources