I have the following model which is wrapped in my view model
public class FloorPlanSettingsModel
{
public int Id { get; set; }
public int? MainFloorPlanId { get; set; }
public string ImageDirectory { get; set; }
public string ThumbnailDirectory { get; set; }
public string IconsDirectory { get; set; }
}
How do I access one of the above properties from Javascript?
I tried this, but I got "undefined"
var floorplanSettings = "#Model.FloorPlanSettings";
alert(floorplanSettings.IconsDirectory);
You could take your entire server-side model and turn it into a Javascript object by doing the following:
var model = #Html.Raw(Json.Encode(Model));
In your case if you just want the FloorPlanSettings object, simply pass the Encode method that property:
var floorplanSettings = #Html.Raw(Json.Encode(Model.FloorPlanSettings));
Contents of the Answer
1) How to access Model data in Javascript/Jquery code block in .cshtml file
2) How to access Model data in Javascript/Jquery code block in .js file
How to access Model data in Javascript/Jquery code block in .cshtml file
There are two types of c# variable (Model) assignments to JavaScript variable.
Property assignment - Basic datatypes like int, string, DateTime (ex: Model.Name)
Object assignment - Custom or inbuilt classes (ex: Model, Model.UserSettingsObj)
Lets look into the details of these two assignments.
For the rest of the answer lets consider the below AppUser Model as an example.
public class AppUser
{
public string Name { get; set; }
public bool IsAuthenticated { get; set; }
public DateTime LoginDateTime { get; set; }
public int Age { get; set; }
public string UserIconHTML { get; set; }
}
And the values we assign this Model are
AppUser appUser = new AppUser
{
Name = "Raj",
IsAuthenticated = true,
LoginDateTime = DateTime.Now,
Age = 26,
UserIconHTML = "<i class='fa fa-users'></i>"
};
Property assignment
Lets use different syntax for assignment and observe the results.
1) Without wrapping property assignment in quotes.
var Name = #Model.Name;
var Age = #Model.Age;
var LoginTime = #Model.LoginDateTime;
var IsAuthenticated = #Model.IsAuthenticated;
var IconHtml = #Model.UserIconHTML;
As you can see there are couple of errors, Raj and True is considered to be javascript variables and since they dont exist its an variable undefined error. Where as for the dateTime varialble the error is unexpected number numbers cannot have special characters, The HTML tags are converted into its entity names so that the browser doesn't mix up your values and the HTML markup.
2) Wrapping property assignment in Quotes.
var Name = '#Model.Name';
var Age = '#Model.Age';
var LoginTime = '#Model.LoginDateTime';
var IsAuthenticated = '#Model.IsAuthenticated';
var IconHtml = '#Model.UserIconHTML';
The results are valid, So wrapping the property assignment in quotes gives us valid syntax. But note that the Number Age is now a string, So if you dont want that we can just remove the quotes and it will be rendered as a number type.
3) Using #Html.Raw but without wrapping it in quotes
var Name = #Html.Raw(Model.Name);
var Age = #Html.Raw(Model.Age);
var LoginTime = #Html.Raw(Model.LoginDateTime);
var IsAuthenticated = #Html.Raw(Model.IsAuthenticated);
var IconHtml = #Html.Raw(Model.UserIconHTML);
The results are similar to our test case 1. However using #Html.Raw()on the HTML string did show us some change. The HTML is retained without changing to its entity names.
From the docs Html.Raw()
Wraps HTML markup in an HtmlString instance so that it is interpreted as HTML markup.
But still we have errors in other lines.
4) Using #Html.Raw and also wrapping it within quotes
var Name ='#Html.Raw(Model.Name)';
var Age = '#Html.Raw(Model.Age)';
var LoginTime = '#Html.Raw(Model.LoginDateTime)';
var IsAuthenticated = '#Html.Raw(Model.IsAuthenticated)';
var IconHtml = '#Html.Raw(Model.UserIconHTML)';
The results are good with all types. But our HTML data is now broken and this will break the scripts. The issue is because we are using single quotes ' to wrap the the data and even the data has single quotes.
We can overcome this issue with 2 approaches.
1) use double quotes " " to wrap the HTML part. As the inner data has only single quotes. (Be sure that after wrapping with double quotes there are no " within the data too)
var IconHtml = "#Html.Raw(Model.UserIconHTML)";
2) Escape the character meaning in your server side code. Like
UserIconHTML = "<i class=\"fa fa-users\"></i>"
Conclusion of property assignment
Use quotes for non numeric dataType.
Do Not use quotes for numeric dataType.
Use Html.Raw to interpret your HTML data as is.
Take care of your HTML data to either escape the quotes meaning in server side, Or use a different quote than in data during assignment to javascript variable.
Object assignment
Lets use different syntax for assignment and observe the results.
1) Without wrapping object assignment in quotes.
var userObj = #Model;
When you assign a c# object to javascript variable the value of the .ToString() of that oject will be assigned. Hence the above result.
2 Wrapping object assignment in quotes
var userObj = '#Model';
3) Using Html.Raw without quotes.
var userObj = #Html.Raw(Model);
4) Using Html.Raw along with quotes
var userObj = '#Html.Raw(Model)';
The Html.Raw was of no much use for us while assigning a object to variable.
5) Using Json.Encode() without quotes
var userObj = #Json.Encode(Model);
//result is like
var userObj = {"Name":"Raj",
"IsAuthenticated":true,
"LoginDateTime":"\/Date(1482572875150)\/",
"Age":26,
"UserIconHTML":"\u003ci class=\"fa fa-users\"\u003e\u003c/i\u003e"
};
We do see some change, We see our Model is being interpreted as a object. But we have those special characters changed into entity names. Also wrapping the above syntax in quotes is of no much use. We simply get the same result within quotes.
From the docs of Json.Encode()
Converts a data object to a string that is in the JavaScript Object Notation (JSON) format.
As you have already encountered this entity Name issue with property assignment and if you remember we overcame it with the use of Html.Raw. So lets try that out. Lets combine Html.Raw and Json.Encode
6) Using Html.Raw and Json.Encode without quotes.
var userObj = #Html.Raw(Json.Encode(Model));
Result is a valid Javascript Object
var userObj = {"Name":"Raj",
"IsAuthenticated":true,
"LoginDateTime":"\/Date(1482573224421)\/",
"Age":26,
"UserIconHTML":"\u003ci class=\"fa fa-users\"\u003e\u003c/i\u003e"
};
7) Using Html.Raw and Json.Encode within quotes.
var userObj = '#Html.Raw(Json.Encode(Model))';
As you see wrapping with quotes gives us a JSON data
Conslusion on Object assignment
Use Html.Raw and Json.Encode in combintaion to assign your object to javascript variable as JavaScript object.
Use Html.Raw and Json.Encode also wrap it within quotes to get a JSON
Note: If you have observed the DataTime data format is not right. This is because as said earlier Converts a data object to a string that is in the JavaScript Object Notation (JSON) format and JSON does not contain a date type. Other options to fix this is to add another line of code to handle this type alone using javascipt Date() object
var userObj.LoginDateTime = new Date('#Html.Raw(Model.LoginDateTime)');
//without Json.Encode
How to access Model data in Javascript/Jquery code block in .js file
Razor syntax has no meaning in .js file and hence we cannot directly use our Model insisde a .js file. However there is a workaround.
1) Solution is using javascript Global variables.
We have to assign the value to a global scoped javascipt variable and then use this variable within all code block of your .cshtml and .js files. So the syntax would be
<script type="text/javascript">
var userObj = #Html.Raw(Json.Encode(Model)); //For javascript object
var userJsonObj = '#Html.Raw(Json.Encode(Model))'; //For json data
</script>
With this in place we can use the variables userObj and userJsonObj as and when needed.
Note: I personally dont suggest using global variables as it gets very hard for maintainance. However if you have no other option then you can use it with having a proper naming convention .. something like userAppDetails_global.
2) Using function() or closure
Wrap all the code that is dependent on the model data in a function. And then execute this function from the .cshtml file .
external.js
function userDataDependent(userObj){
//.... related code
}
.cshtml file
<script type="text/javascript">
userDataDependent(#Html.Raw(Json.Encode(Model))); //execute the function
</script>
Note: Your external file must be referenced prior to the above script. Else the userDataDependent function is undefined.
Also note that the function must be in global scope too. So either solution we have to deal with global scoped players.
try this: (you missed the single quotes)
var floorplanSettings = '#Html.Raw(Json.Encode(Model.FloorPlanSettings))';
Wrapping the model property around parens worked for me. You still get the same issue with Visual Studio complaining about the semi-colon, but it works.
var closedStatusId = #(Model.ClosedStatusId);
I know its too late but this solution is working perfect for both .net framework and .net core:
#System.Web.HttpUtility.JavaScriptStringEncode()
For MVC 4
#model Inventory.Models.PurchaseVModel
#{
ViewData["Title"] = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
var jsonItems = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model.Items);
}
#section scripts{
<script>
$(document).ready(function () {
var allItem = #Html.Raw(jsonItems);
console.log(allItem);
});
</script>
}
.Net Core 3.1
#model Inventory.Models.PurchaseVModel
#{
ViewData["Title"] = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
var jsonItems = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Items);
}
#section scripts{
<script>
$(document).ready(function () {
var allItem = #Html.Raw(jsonItems);
console.log(allItem);
});
</script>
}
I used the following to convert a Model object passed from the controller in the Razor view to JSON Object in JavaScript. This worked perfectly for me in ASP.Net Core.
<script>
let jsonData = #Html.Raw(Json.Serialize(Model))
console.log(jsonData)
</script>
In addition to #Rajshekar's thorough answer, I prefer a helper method that returns a IHtmlString value, which Razor recognizes as already-encoded HTML. Then, the Html.Raw() wrapper isn't needed, improving readability.
public static class JSHelper
{
/// <summary>
/// Encode a value as a JSON value or object for use in JavaScript code.
/// </summary>
/// <param name="value">Value to encode.</param>
/// <returns>HTML string containing the JavaScript value or object.</returns>
public static IHtmlString JsonEncode(object value)
{
return MvcHtmlString.Create(Json.Encode(value));
}
}
Then I can just use, obj = #JSHelper.JsonEncode(myObject); in my JavaScript section.
Include a using clause in your view or update view/web.config to resolve the namespace within the view.
So here is how we do it in .net core razor pages.
#section scripts
{
<script>
$(document).ready(function () {
leaveStatusDictionary = #Html.Raw(Json.Serialize(Model.LeaveStatusDictionary));
});
function GetStatusName(status) {
return window.leaveStatusDictionary[status];
}
</script>
}
Note the following.
I did not use var keyword before leaveStatusDictionary. So this now a property on the window object. So I was able to access that in a different js function - GetStatusName you see there.
LeaveStatusDictionary is a property on the model class of type Dictionary<int, string>.
If "ReferenceError: Model is not defined" error is raised, then you might try to use the following method:
$(document).ready(function () {
#{ var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var json = serializer.Serialize(Model);
}
var model = #Html.Raw(json);
if(model != null && #Html.Raw(json) != "undefined")
{
var id= model.Id;
var mainFloorPlanId = model.MainFloorPlanId ;
var imageDirectory = model.ImageDirectory ;
var iconsDirectory = model.IconsDirectory ;
}
});
Related
I just want to check if my RepoItemInfo object (which is username Joe McAdam btw) is equal to string data.
I just tracked this element in Chrome, stored it in my repository and it is a span with #innertext="JoeMcAdam"
Then I made code module for mapping these objects in C# code:
public RepoItemInfo LeftNameRepoItem {get {return _pageHome.Home.ImeLijevoInfo; }}
And I have prepared data Name in my context file for this LeftName to check if they are equal:
void ITestModule.Run()
{
var dto = new LoginDto () {
Name = "Joe McAdam",
WaitTimeLimit = 20000,
};
LoginDtoContext.template = dto;
}
I just need a proper code example for checking if they are eqal. What should I do? Do I have to make some adapter for this RepoItemInfo to convert it in string, text or something else?
I hope I provided enough details about my problem.
Thanks in advance!
Working in a C# Asp.netcore project, I'm trying to read an array from an IQueryCollection in a GET request. The IQueryCollection is in Request.Query.
I need to read the query without a model. When the front end json hits the back end, its no longer JSON which is fine, I just need to read whatever the front end passed. In this example its a int array but it could be anything.
Models don't scale very well for queries. To return a model on a get, they work brilliantly but for a query that can be as complex or simple as it needs to be I can't commit to models. Would be really usefull if I could extract the query to an anonamous object.
Json passed from the front end:
var params = {
items: [1, 4, 5],
startDate: new Date(),
endDate: new Date()
}
C# Request.QueryString
{?items%5B%5D=1&items%5B%5D=4&items%5B%5D=5&startDate=Fri%20Oct%2015%202021%2022%3A30%3A57%20GMT%2B1000%20(Australian%20Eastern%20Standard%20Time)&endDate=Fri%20Oct%2015%202021%2022%3A30%3A57%20GMT%2B1000%20(Australian%20Eastern%20Standard%20Time)}
I've tried:
// Gives me a null
var value = HttpUtility.ParseQueryString(Request.QueryString.Value).Get("items");
// Gives me an empty object {}
var value = Request.Query["items"];
Hope this is enough information.
The query string format is undefined for arrays. Some web frameworks use ?foo[]=1&foo[]=2, others use ?foo=1&foo=2, again others ?foo[0]=1&foo[1]=2.
You'll have to use the same parsing serverside as you serialize it clientside. This works for your [] syntax:
var queryString = "?items%5B%5D=1&items%5B%5D=4&items%5B%5D=5&date=2021-10-15";
var parsed = HttpUtility.ParseQueryString(queryString);
foreach (var key in parsed.AllKeys)
{
if (key.EndsWith("[]"))
{
var values = string.Join(", ", parsed.GetValues(key));
Console.WriteLine($"{key}: array: {values}.");
}
else
{
Console.WriteLine($"{key}: scalar: {parsed[key]}.");
}
}
Output:
items[]: array: 1, 4, 5.
date: scalar: 2021-10-15.
But instead of parsing the query string yourself, let the framework do that. You say you don't find models scalable; I find that hand-crafting code doesn't scale well. A model like this would just work, granted you fix your date serializer on the JS side:
public class RequestModel
{
public int[] Items { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
I have following input field.
<input class="book_now_modal BClick" type="text" id="destinationFrom_modal" name="destinationFrom_modal" placeholder="#ViewBag.AmritsarList[0].Destination">
time to time I want to change the value inside the view bag.is there a way to give dynamic value to the viewbag item.I have tried with jquery as below.
$(".AClick").click(function () {
//$(".BClick").attr("placeholder"," ");
var s = parseInt($(this).attr('id'));
var neededVAl = "##ViewBag.AmritsarList"+"["+s+"]"+".Destination";
alert(neededVAl);
var b = $(".BClick");
$(this).attr("placeholder",neededVAl);
});
like this I replace the placeholder as an alert it gives #ViewBag.AmritsarList[1].Destination but it didn't change the placeholder.how can I do that.
var neededVAl = "##ViewBag.AmritsarList"+"["+s+"]"+".Destination";
You can't access the ViewBag using above statement, since it accessible on server side. Your JavaScript statement is executed on the client side and can't directly access it.
However, You can use Json.Encode Method converts a data object to a string that is in the JavaScript Object Notation (JSON) format.
var jsObject = #Html.Raw(Json.Encode(ViewBag.AmritsarList));
$(".AClick").click(function () {
var s = parseInt($(this).attr('id'));
var neededVAl = jsObject[s].Destination;
alert(neededVAl);
$(this).attr("placeholder",neededVAl);
});
I am writing a backend for SlickGrid. I have an AJAX call that loads the column definitions from my MVC Controller. The controller looks like this:
Public ActionResult GetColumns(...){
List<object> columns;
foreach(var col in MyColumns){
columns.Add(new { id = col.id, name = col.name, width = col.width});
}
return Json(new { columns = columns });
}
One of the column attributes that slickgrid accepts is formatter, which takes a js function name.
Unfortunately, MVC puts that name in double quotes. I need to figure out if there is a way to serialize just that one field without quotes.
eg (this is what I want)
[{"id":1,"Name":"FirstName","width":50,"formatter":NameFormater},...]
vs (ths is what I am getting now)
[{"id":1,"Name":"FirstName","width":50,"formatter":"NameFormater"},...]
I know this isn't proper JSON Format to pass JS Code in it(Proper JS Format though). But I have a valid use case here and am trying not to add too much complexity.
You could wrap the word NameFormater with special characters such as
[{"id":1,"Name":"FirstName","width":50,"formatter":"#NameFormater#"},...]
and when you get your results back, if you are using javascript
function(result){
result = result .replace(/"#/g, "");
result = result .replace(/#"/g, "");
// result now = [{"id":1,"Name":"FirstName","width":50,"formatter":NameFormater},...]
// ...Pass result to SlickGrid
}
I'm serializing my model:
<script type="text/javascript">
var Model = '#Model.ToJson()';
</script>
the ToJson is an extension method:
public static string ToJson(this object obj)
{
var serializer = new JavaScriptSerializer();
var val = serializer.Serialize(obj);
return val;
}
Now I want to access my model in other javascript files by doing:
var hello = Model.id;
The problem is that it doesn't serialize correctly when I use '#Model.ToJson()' because of the quotes.
The serialized object looks like this:
var Model = "{ "id": "2231f87c-a62c-4c2c-8f5d-b76d11942301" }";
But in order for me to access id by Model.id it should look like this:
var Model = { "id": "2231f87c-a62c-4c2c-8f5d-b76d11942301" };
How can I enter razor syntax without quotes? Using asp.net syntax I think it's:
var Model = <%=Model.ToJson() %>
How do I do the same with razor? Thanks!
If you use this JSON plugin you can do it all on the client and simply things. If you push up a JSON string, in javascript you could then do:
<script type="text/javascript">
var Model = JSON.parse("#Model.JsonString");
</script>
Since Razor by default escapes out the quotes, you need to use Html.Raw:
var Model = #Html.Raw(Model.ToJson());
It will turn that into:
var Model = {"id":"whatever"};
which is valid javascript.