1

How can I get the value of the first item from the model's List? My ViewModel:

namespace TestWeb
{
 public class NavViewModel
 {
 public NavViewModel()
 {
 NavStepList = new List<NavStep>();
 }
 public List<NavStep> NavStepList { get; set; }
 }
}

In my View:

@using TestWeb
@model NavViewModel
<script>
 $(document).ready(function () {
 var Testvalue = @Model.NavStepList.First();
 });
</script>

I want the first value of NavStepList. However an error occurs when I call this function:

TestWeb is undefined

I already test with testing value assign to NavStepList but the error still occurs.

My NavStep Class Property:

public class NavStep{
 public int NavStepId { get; set; }
 public int SortId { get; set; }
 public string StepName { get; set; } 
 public string IdentifierKey { get; set; } 
 public bool IsAvailible { get; set; }
}
Anil
3,7522 gold badges27 silver badges50 bronze badges
asked Apr 3, 2017 at 9:09
4
  • You're trying to coerce a class to a string. You need to retrieve the properties of the class and place them in separate JS variables. Commented Apr 3, 2017 at 9:10
  • @RoryMcCrossan Please can you explain more detail? Commented Apr 3, 2017 at 9:12
  • I'd need to see the properties of your NavStep class to give you a definitive answer. Commented Apr 3, 2017 at 9:13
  • You can remove @using TestWeb line and then check. Commented Apr 3, 2017 at 9:14

3 Answers 3

3

You could convert your server side view model to a javascript variable using the following:

<script>
 $(document).ready(function () {
 var myModel = @Html.Raw(Json.Encode(Model.NavStepList.First()));
 });
</script>

and then you could access your model properties in javascript:

alert(myModel.NavStepId);
alert(myModel.SortId);
alert(myModel.StepName);
...
answered Apr 3, 2017 at 9:44

Comments

1

Here @Model ought to refer to NavViewModel directly. Also, as Rory said, trying to assign a class, NavStep to a javascript variable isn't going to work out. What you probably want is:

var navStepId = @Model.First().NavStepId;
var sortId = @Model.First().SortId;
var stepName = "@Model.First().StepName"; //wrap strings in "" so javascript recognizes them as such
var identifierKey = "@Model.First().IdentifierKey";
var isAvailable = "@(Model.First().IsAvailable)" === '@true'; //method to convert c# true to javascript true
answered Apr 3, 2017 at 9:14

6 Comments

NavStepProperty model is not mention any where
I want all property of NavStep.
It's just an example I made up, I don't know what's in the class NavStep. Presumably var TestValue ought to be pointing to one of it's properties, (e.g. NavStepProperty)
@Jze in that case you'll probably want a couple of javascript variables to assign each property to, var testvalue1 = ... ; var testvalue2 = ...; edit - have updated my answer since NavStep was included in question
@ButtDrivenDevelopment Thanks.I appreciate your suggestion.
|
1

Case

The @NavViewModel model exists on server.
jQuery script are running on client (browser)

Problem
How to access server side object on client?

Solution : Serialize sever side object to client

As we can not directly access server-side objects on client We need to serialize the model (full/part).

var flatmodel = [@Html.Raw(Json.Encode(Model.NavStepList.First()))][0];

Now flatmodel is JavaScript variable and we can access property via flatmodel.

answered Apr 3, 2017 at 9:47

3 Comments

Thanks,Can i use that json object in ajax.When i use this json object in ajax,IE suddenly crush." Illegal invocation" error message show in Chrome.
how are you using this in ajax, can you please add that code part also, before also try to add processData: false to ajax call?
@Jze Update in my answer can solve your problem, further you can refer stackoverflow.com/questions/17617263/… and stackoverflow.com/questions/28095567/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.