0

My Model is Like this:

public class ViewMessageFileModel
    {
        [Display(Name = "FileOptionId")]
        public int ? FileOptionId { get; set; }


        [Display(Name = "LeaveInMyInbox")]
        public bool ? LeaveInMyInbox { get; set; }



        IList<int> _fileforMyTeam;
        public IList<int> FileForMyTeam
        {
            get
            {
                if (_fileforMyTeam == null) _fileforMyTeam = new List<int>();
                return _fileforMyTeam;
            }
            set { this._fileforMyTeam = value; }
        }

         IList<int> _fileforfacility;
        public IList<int> FileForFacility
        {
            get
            {
                if (_fileforfacility == null) _fileforfacility = new List<int>();
                return _fileforfacility;
            }
            set { this._fileforfacility = value; }
        }
        //Similarly for other option   

        private IList<ViewUserGroupModel> _careTeam;
        public IList<ViewUserGroupModel> CareTeamForFile
        {
            get
            {
                if (_careTeam == null) _careTeam = new List<ViewUserGroupModel>();
                return _careTeam;
            }
            set { this._careTeam = value; }
        }

    }

I want to use this model in my view as follows

1-Use of care careteamforfile list directly in view iterating through @Model.CareTeamForFile .But I want to use Fileforfacility and fileformyteam List to store in a json variable in view based on the value of which i have to select or deselect some checkboxes

Is this possible for me to convert the Model element to json on view page. Also suggest me the best approach.

3 Answers 3

3

You can use @Html.Raw(Json.Encode(Model.CareTeamForFile)) to convert your list to JSON and return it as raw HTML. Then use JSON.parse() to convert it to a JavaScript object.

<script type="text/javascript">
    var json = "@Html.Raw(Json.Encode(Model.CareTeamForFile))";

    var model = JSON.parse(json);

    ...
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

I have to convert json for Model.Fileforfacility in view if I use the same model = "@Html.Raw(Json.Encode(Model.FileForFacility))"; alert(model[0])its not giving the int value saved in it
You need to parse the JSON before you use it, otherwise it's just a string var parsed = JSON.parse(model);
1

Razor can parse the following just fine:

<script type="text/javascript">    
    $(document).ready(function() {
      alert("@Model.CareTeamForFile ");
    }    
</script>

1 Comment

I want to get int value saved in List so what will be the syntax for that
1

Use single quotation for use model in mvc Razor

For example :

$('#txtID').val('@Model.ID');

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.