ViewBag in JQuery - asp.net-mvc

I am using ViewBag value as follows :
var date = "#ViewBag.fromDateForEditMode"
$('#FromDate').val(date);
All I am getting is #ViewBag.fromDateForEditMode and no value.

The Razor engine doesn't execute when MVC is rendering an HTML file, so nothing will parse #ViewBag.fromDateForEditMode and replace it with the ViewBag property value :)

The problem that you are having is that you are trying to enclose the #ViewBag.fromDateForEditMode in quotes. When you do that within your jquery function, this causes the browser jquery processor to evaluate it as a literal string at the time the script is called, rather than allowing your razor engine to evaluate the ViewBag contents at the time the page is loaded.
To get around this issue, use:
var startDate = new Date(#ViewBag.StartDate.Year.ToString(), #ViewBag.StartDate.Month.ToString() -1, #ViewBag.StartDate.Day.ToString());
This will allow the razor engine to evaluate the ViewBag contents before the jquery script is executed.

Related

How do I get a custom localized string in the front end?

I'm having trouble accessing localizations, the value always appears in the default language.
I have two resource files in App_GlobalResources:
Resource.resx contains a string with the name "Hello" and the value of "World".
Resource.fr.resx contains a string with the name "Hello" and the value of "Monde"
In my Razor template:
<lang:string key="Resource, Resources.Resource.Hello" xmlns:lang="http://www.composite.net/ns/localization/1.0" />
"World" is always displayed, never "Monde", even when viewing the page in French.
In a vanilla asp.net website template, with the same resources, the following code within a Razor template displays "Monde"
String cultureInfo = "fr-CA";
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture(cultureInfo);
System.Threading.Thread.CurrentThread.CurrentUICulture = new
System.Globalization.CultureInfo(cultureInfo);
Response.Write(ObjectInfo.Print(Resources.Resource.Hello));
However, within C1 it results in "World".
How should I be going about this?
Thanks!
The <lang:string construct is mainly meant for XSLT templates and functions where you can't as easily fallback to calling c# code directly as in Razor.
Have you tried printing out the resource on your Razor template like this instead #Resources.Resource.Hello ?

Html.Textbox helper not setting the value of textbox in MVC5 Razor

I am using MVC5 ASP.Net with Razor Engine for a view that has following html helper for displaying a textbox and setting its value to ViewBag.SearchText.
When user inputs text into this textbox and view posts back, then the value is set correctly according to value in ViewBag.SearchText, but when in controller's action the ViewBag.SearchText = string.Empty, then textbox still retains its original value rather than an empty string.
Is this how the Html helper for textbox is supposed to behave Or I am missing something, and how could I set the value of textbox to an empty string? This is quite strange since the ViewBag.SearchText value is an empty string when I step through code in Visual Studio 2013.
#Html.TextBox("SearchText", (string)ViewBag.SearchText)
UPDATE 1:
I found that using the code Model.Clear() at start of the controller action solved this problem. However, I found another way to just clear that part of ModelState that is used to populate the textbox after postback. The key in ModelState is the id of the textbox, which is 'SearchText'.
The code for this is as below.
ModelState.SetModelValue("SearchText", new ValueProviderResult(null,string.Empty,
System.Globalization.CultureInfo.InvariantCulture));
That's by default (ModelState retains the value on post back).
You can use ModelState.Clear() before setting ViewBag.SearchText = ""; and returning the view. However use with caution because clearing ModelState can have unexpected results if your using validation

Can we Assign Javascript variable value to Razor Syntax Variable?

Can we Assign Javascript variable value to Razor Syntax Variable?
like.
var Javascript-variable=$("#test").val();
#html.Actionlink("User","Index",new {Id='Javascript-variable'})
Can we Passing Java script variable value to Razor Syntax Variable?
No, because JavaScript used in this context executes on the client, whereas Razor code executes on the server. For practical purposes, they have no relation.
But I imagine that you want to use the routing capabilities provided on the server from within JavaScript, and that is possible. We can use Razor to generate JavaScript.
Embed the ActionLink() call into a JavaScript snippet.
Render the URL structure on the server, using the ActionLink() method which is route-aware.
Get a value on the client.
Insert the value into the URL structure.
One way to do that is to render the link with a placeholder value, which is later replaced using JavaScript.
var url = '#Html.ActionLink("User","Index",new { Id = 0 })';
var id = $("#test").val();
var mergedUrl = url.replace(/0$/, id); // replace the 0 with a real ID

Kendo UI Grid ASP.NET MVC Wrapper ParameterMap

Is there a way to define the parameterMap option for a Kendo UI Grid via the server side ASP.NET MVC wrappers?
I need to change local time into UTC time before sending up a filter command to the server, and this appears to be the only way to do it.
Sort of...
You can specify a string as the parameterMap setting, and this string can either be a JavaScript function directly, or the name of a JavaScript function that is found on the page.
.parameterMap("myParamMapFunction");
or
.parameterMap("function(data){ /* do stuff with the data */}");

Asp.net mvc html.raw doesnt resolve

#Html.Raw(HttpUtility.HtmlDecode(Model.Id.ToString()))
always return 0 in the Index.cshtml file placed under "EditorTemplates" folder
Am i doing anything incorrect
I have the model defined at the top of the page as
#model [Namespace].ViewModel
If the id is numeric, there is no reason to use Html.Raw at all. There is no HTML code in the value that should aviod the normal HTML encoding.
Just use:
#Model.Id
The value will implicitly be converted to a string, so the ToString call is not needed either.
If it doesn't show the expected value, you know that it wasn't there in the first place.

Resources