Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im just learning to get acquainted to .net core. Right now trying out a small application in asp.net core, MVC ver. 3.1.3.

From the Maincontroller's action method, the view is called :

public ViewResult mainMenu()
{
    MenuInfoModel menuInfoModel = new MenuInfoModel(); 
        return View("smnu",menuInfoModel);
}


The ViewModel for the View is :

public class MenuInfoModel
    {   
        public String getLicensedName()
        {
            return "Natasha Park";
        }
    }


The view(smnu.cshtml) content :

@model  LS.Models.ViewModels.MenuInfoModel    
@{
    ViewBag.Title = "LS - System";
}
<div Id="license">Licensed To : </div>
@section Scripts
{
    <script>
        $('#license').text("Licensed To : " + @Model.getLicensedName());
    </script>
}


The _Layout file is :

<!DOCTYPE html>

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width" initial-scale=1 charset="utf-8" />
    <link href="~/lib/twitter-bootstrap/css/bootstrap.css" rel="stylesheet" />
    <script src="~/lib/jquery/jquery.js"></script>
    <script src="~/lib/twitter-bootstrap/js/bootstrap.js"></script>
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
@if (IsSectionDefined("Scripts"))
{
    @RenderSection("Scripts", required: true)
}
</html>


In the _ViewStart i have assigned the layout file to "Layout"

My Issue is :

The text for License is not changing. There are no errors. If i change the text directly in the script.. it happens but not through the return value from the ViewModel. How to make this change to take effect.

Please help.

Thanks in advance.

What I have tried:

Tried to attach a string in the script straight away it works.
Posted
Updated 21-May-20 7:06am
Comments
#realJSOP 4-May-20 8:24am    
put a breakpoint in your viewmodel method to see if it's even getting there.

Quote:
There are no errors.
I'd be amazed if that was the case!

Check your browser's developer console, and you'll see that you have a syntax error in the rendered Javascript:
JavaScript
$('#license').text("Licensed To : " + Natasha Park);
You have an unquoted string literal, which is not valid Javascript.

If you want to emit a string variable from the server into your Javascript, you need to encode it properly. Fortunately, that's quite easy to do in ASP.NET Core:
Razor
@section Scripts
{
    <script>
        $('#license').text("Licensed To : " + @Json.Serialize(Model.getLicensedName()));
    </script>
}
This should output:
JavaScript
$('#license').text("Licensed To : " + "Natasha Park");
which shouldn't produce any client-side errors.

But, as solution 1 said, you don't need to use Javascript to output server-site values in your markup. Just output the value directly where you want it to appear:
Razor
<div id="license">Licensed To : @Model.getLicensedName()</div>
ASP.NET will ensure that the value is properly HTML-encoded, so you won't get any nasty surprises if the returned value contains "special" characters.

Also, as solution 1 mentioned, in .NET it's preferable to use properties instead of Java's getter and setter methods.
Properties - C# Programming Guide | Microsoft Docs[^]
 
Share this answer
 
Comments
Maciej Los 21-May-20 16:32pm    
5ed!
You are trying to use a method to show the licence name and not a property. Try:

public class MenuInfoModel
{
public string LicensedName { get; set; }
}

with:

public ViewResult mainMenu()
{
MenuInfoModel menuInfoModel = new MenuInfoModel();
menuInfoModel.LicenceName = "Natasha Park";
return View("smnu",menuInfoModel);
}

and change your view to:

@model LS.Models.ViewModels.MenuInfoModel
@{
ViewBag.Title = "LS - System";
}
<div>Licensed To : @Model.LicenceName</div>

MVC will replace @Model.LicenceName with the name held in the model. There is no need to use JQuery! You could also use:

<div>
<label>Licenced To : </label>
@Html.Display("LicenceName")
</div>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900