Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dipa 88 posts 370 karma points
    Jun 01, 2016 @ 12:18
    Dipa
    0

    ViewData to list in view page.

    Hi,

    In my application, I have one ActionResult method in surface controller. From that it is passing list through ViewData. In view page, it is showing data in Quick Watch properly.

    But now I am not able to convert it to list or any other type, so that using for loop I can show it on page.

    Please suggest me how to handle ViewData in partial view? Please guide me.

    Or guide me for handling list from surface controller.

    Thanks, Dipa

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Jun 01, 2016 @ 14:07
    Michaƫl Vanbrabandt
    100

    Hi,

    A better aproach would be to create a new ViewModel which is just a class that contains your list property and pass this into your PartialView.

    Code not tested

    Create a new class in your Models folder:

    public MyViewModel {
        public List<string> List { get; set; }
    }
    

    In your surface controller:

    public ActionResult Index() {
        MyViewModel _model = new MyViewModel() {
            List = new List<string>()
        };
    
        return Partial("PartialView", _model);
    }
    

    And then in your partial view:

    @Model MyViewModel
    
    @foreach(var item in Model.List)
    {
         // do stuff
    }
    

    /Michael

Please Sign in or register to post replies

Write your reply to:

Draft