I use aspx c# as a viewengine within ASP.NET mvc project, I want to retrieve value from viewbag using javascript code.
I get all my viewbag related stuff from in a razor view like so ...
<script>
var myJSVariable = #Viewbag.MyViewbagVariable;
</script>
You could do the following for the MVC view engine pre-razor I believe
<script>
var myJSVariable = <%Viewbag.MyViewbagVariable%>;
</script>
<script type="text/javascript">
var yourVariable= #Html.Raw(Json.Encode(ViewBag.yourVariable))
</script>
If you are dealing with something simple like a string, you can save a viewbag in an data-* attribute of a HTML tag and use JavaScript to access that.
Related
I'm trying to integrate CKEditor with my application and I'm using the below approach.
<textarea name="editor1" id="editor1" rows="10" cols="80"></textarea>
<script type="text/javascript">
CKEDITOR.replace('editor1');
</script>
and in JavaScript to set and get data to ckeditor I'm using the code as below
function cksetdata(val)
{
CKEDITOR.instances.editor1.setData(val);
}
var data = CKEDITOR.instances.editor1.getData();
It is working perfect when using JavaScript.
but I want to set and get data from code behind as I want to save the data of CKEditor into database.
If using the CKEditor as control in aspx page I'm able to retrieve data by using .Text property of CKEditor, but unable to get data through JavaScript.
I need to retrieve data from both JavaScript and codebehind.
Thanks for ur answer Mr.Raymond kuipers..
Im using a work around for this problem..
As im able to retrieve data in javascript im assigning that data to a hidden variable and accessing the value of that hidden variable in my button save event..
function getCkEditordata() {
document.getElementById('<%=hdn1.ClientID%>').value = CKEDITOR.instances.editor1.getData();
alert(document.getElementById('<%=hdn1.ClientID%>').value);
}
in this way assigning to a hidden variable and accessing that data in code behind as follows..
String templatecontent = hdn1.Value;
use the CKEeditorForAspNet nuget package and you will have a normal control in aspx. You can than set and get the data using the .Text property.
I have a suite of several MVC3 web applications, all of which reference a common Core.dll. I have compiled common views using RazorGenerator, and the subscribing sites find the relevant views from the pre-compiled .dll without any problem.
I am trying to do the same for the layout page, as this is common across all the sites too, save for one or two divs that are specific to that particular site. This also works fine, in as much as the _layout view is served up by just doing this:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
But, to get the site specific divs in the layout populated, I want to have a partial view in the specific site and use JQuery to set the HTML of the placeholding div in the _layout. Something like:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
//Have a hidden div containing the partial view that sits in the specific site
<div id="SiteSpecificStuff" style="display:none">
#Html.Partial("_SiteSpecificStuff", model)
</div>
// Use jQuery to populate the html of the placeholding div on the _Layout
// with that of the partial view
<script type="text/javascript">
$(document).ready(function () {
$("#divPlaceHolderOnLayout")
.html($("SiteSpecificStuff").html());
});
</script>
I have tried this but the _ViewStart does not re-fire on every post. Is this possible using a different approach?
I think you're looking for this:
#RenderSection("YouSection", required: false)
How could I display an alert from the controller class in CodeIgniter?
Typically you want to place any display content (such as HTML or Javascript) in a view, not in a controller. From the controller you load the view, and the view contains this code somewhere in it:
<script type="text/javascript">
alert('your alert');
</script>
See the CodeIgniter user_guide for more basics on how to structure your application:
http://www.codeigniter.com/user_guide
Anything you "print" using PHP's print, displays to the screen.
print "<script type=\"text/javascript\">alert('Some text');</script>";
Anybody? There is another question regarding this but the only answers were to code up some javascript validation, which also refuses to work on my partial view ("$ is not defined").
Anyway, I don't want to use javascript I just want simple validation for required fields that cannot be left blank, number fields that require ints, etc.
Can anyone shed some light on validation and partial views?
I suspect that you are loading those partial views using AJAX. If this is the case you will need to manually invoke the $.validator.unobtrusive.parse method once you inject the new contents of the partial into the DOM as explained in this article.
Brad Wilson also discussed this in his blog post:
The unobtrusive client validation script automatically parses the
initial set of HTML for validation rules when the page has finished
loading. If your page dynamically adds new HTML content (perhaps
throught Ajax or through client-side application code), you may wish
to parse that new HTML for client validation on the new HTML elements.
To parse new HTML, you can call the
jQuery.validator.unobtrusive.parse() method, passing it a selector for
the HTML that you would like to be parsed. You can also call the
jQuery.validator.unobtrusive.parseElement() function to parse a single
HTML element.
As far as the $ is not defined error you should make sure that you have included the proper scripts:
<script src="#Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
Also make sure you are not referencing any of the Microsoft*.js scripts. They are obsolete and should no longer be used in ASP.NET MVC 3.
Of course that's only a supposition, you haven't shown any code so we cannot know what you are doing.
I'm having the same problem and i found that it's not possible to call $.validator.unobtrusive.parse() on the same form twice.
When loading the form initially from the server the form is parsed automatically by the unobtrusive library. When you add an input element dynamically to the form and call $.validator.unobtrusive.parse() again, it won't work. The same goes for parseElement().
So before you call $.validator.unobtrusive.parse, remove the original validator and unobtrusive validation from the form like so:
success: function (html) {
$("#div-id").append(html);
var form = $("#div-id").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse($("#editorRows"));
}
I have a html page in which i have a div where i need to load content from another page, which contains javascript and html. if i load as plain html, the html doesn't work.
so is there anyway make the javascript to work the way it has to and the html too when loading it using ajax.
<script type="text/javascript">
$(document).ready(function(){
$("#myDiv").load("myPage.html");
});
</script>
If I'm reading this properly, then you might not be-able to do this, there are all sorts of mechanisms to stop javascript from accessing data from other places, due to security concerns.
However, can you not simply just use an IFrame ?
$(document).ready(function(){
$('#result').load('ajax/test.html'); });
else try this
$('#result').load('ajax/test.html', function() {
alert('Load was performed.'); });