简体   繁体   中英

Javascript JSON Parsing from ViewModel in MVC “Unexpected Token &”

There are a lot of questions hanging around on this of which I've already tried all the answers I can find. I am brand new to Javascript/JSON and I cannot for the life of me figure out what is going wrong here.

So, in my controller I have this:

var mapVM = new MapVM();
var vehicleVM = Mapper.Map<List<Vehicle>, List<VehicleVM>>(vehicles).ToList());
//Automapper here ^

var jsonVehicleVM = JsonConvert.SerializeObject(vehicleVM);

mapVM.Vehicles = jsonVehicleVM;

//At this point mapVM.Vehicles is perfect JSON and displays in Visual Studio in JSON View
return View(mapVM);

In the view, I have this:

var jsonString = @Model.Vehicles;
var vehicles = JSON.parse(jsonString);

But it looks like:

var jsonString = [{&quot;ShortReg&quot;:&quot;*Android Phone&quot;,&quot;Reg&quot;:&quot;*Android - Kate (Galaxy S3)&quot;,&quot;LatestLoca.......... ETC

Now, I'm guessing it's throwing a fit because of those " bits in there.

I've tried the whole .replace() thing, didn't work. I am frustrated because I'd rather be able to run a loop in javascript on the items than having to write @: for every new line of C# code I'm looping through if I want it to output to the page.

正如devqon建议的那样:

var jsonString = @Html.Raw(Model.Vehicles);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM