0

I have a class like below:

public class CustomBenefitCommittee
{
    public bool? IsChecked { get; set; }
    public bool PropertyError { get; set; }
}

And I am using this class as a list type in MVC model like:

@model List<CustomBenefitCommittee>

I want to retrieve data from this model in jQuery. I am trying right now as

@{
    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")
{
    $.each(model, function () {
        alert(model.PropertyError );
    });
}

This is giving me undefind. Can someone help me out getting the values from this model.

5
  • 1
    var model = @Html.Raw(Json,Encode(Model)); $.each(model, function(index, item) { alert(item.PropertyError); }); Commented Mar 31, 2017 at 11:05
  • @StephenMuecke var model = @Html.Raw(Json,Encode(Model)); This part gives an error of ;, Json and Encode. Commented Mar 31, 2017 at 11:32
  • Json.Encode (dot not comma - typo in comment above). And the syntax error for the ; can be ignored - its just VS not recognizing perfectly valid code. Commented Mar 31, 2017 at 11:35
  • 1
    @MAdeelKhalid, You added the correct solution in your edit (OP's issue is that model.PropertyError is undefined because model is the collection, not an item in the collection) but you immediately deleted it :) - it was your original answer that was wrong Commented Mar 31, 2017 at 11:40
  • I thought I was wrong that's why I removed the answer, I undo that delete. Commented Mar 31, 2017 at 11:47

3 Answers 3

1

Make a loop like this:

 $.each(model, function(key, value) { alert(value.PropertyError); });

It will work.

Sign up to request clarification or add additional context in comments.

5 Comments

I could be wrong, can you explain the reason for downvoting, I might learn something which I don't.
Converting it to a string and then parsing it back again is pointless.
I got it. That was my thought too before I was testing but whenever I do JSON.parse, it gives correct result but with only @Html.Raw It doesn't. There could be some other issue.
Just delete the original bit (which is pointless) - its the last code snippet which is correct.
Edited the answer and thumbs up for you for the correction.
1

Make an method in you controller that returns model list (in json format) like:

public JsonResult LoadList()
{
   var modelList=new List<CustomBenefitCommittee>();
   modelList=   //get your list of model
   var jsonResult = Json(modelList, JsonRequestBehavior.AllowGet);
   return jsonResult;
}

Now on View call this method throught jquery like:

<script> 
 $(document).ready(function() {
 $.getJSON('/"Controller Name"/"Action Name as "LoadList', function (data) {

 if (data.length == 0) {
 alert('No data');
 }
 else{
 $(data).each(function() {
 alert(this.PropertyError);
 });
 }

 });

});
</script>

Comments

0

From Server Side: First you need to add actions in the controller returns JsonResult.

public JsonResult GetData(int id)
{
    var data = new 
    {
      id,
      age = 23
    }
    return Json(data,JsonRequestBehavior.AllowGet);
}

From Client Side: you need ajax call to retrieve the data

function getData(id){
   var data = {id: id};
   $.get('/Home/GetData/', data,
         function (response) {
             alert(JSON.stringify(response));
             },'json'
    };
}

OR if you are sending data to model and you want to use it in your javascript or jquery code you can use NewtonSoft.Json.

First install the Package in your solution:

PM> install-package NewtoSoft.Json

Second you can make your view like this:

@model ...
@using Newtonsoft.Json

<script type="text/javascript">

    var data = @Html.Raw(JsonConvert.SerializeObject(this.Model));

</script>

than you can use this data like this:

$.each(model, function(index, item) { alert(item.PropertyError); });

And also you can use one the javascript frameworks like Konockout.js or angularjs

Comments

Your Answer

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.