Link to home
Start Free TrialLog in
Avatar of websss
websssFlag for Kenya

asked on

create Dynamic data in partial view, and return html

I'm trying to load html data into a modal window

I've got it working, however I now need to pass in a ID to this to return the correct record

I currently have this code which is working
$.ajax({
    type: "POST",
    async: false,
      url: "@Url.Action("CameraImage", "Home")",
    data: "{'myId': '" + this.specialId + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function (data) {
        $("#modal-body").html(data);
        $("#myModal").modal("show");
    },
    error: function () {
        alert('error!');
    }
})

Open in new window


and then in the homecontroller
 [HttpPost]
        public PartialViewResult CameraImage(int myId)
        {
            //var requestedImageData = processData.LoadGraphImageData(myId);
                
            return PartialView("~/Views/Home/CameraImage.cshtml");
        }

Open in new window


This is working fine and returns the data into the popup modal window

Its the next bit i'm stuck on
I need to pass in the ID and pull out the data relevant to that ID and display in the popup modal

I've already got the ID passed to the home controller, but i'm unsure how to display the html

in the home controller, this line gets a datetime, image location and some other strings of data
var requestedImageData = processData.LoadGraphImageData(myId);

Open in new window


I would this like to return some formatted html i.e.
<div id="image">image pic goes here</div>
<div id="imageDate">12th jan 2016</div>
<div id="otherString">otherString goes here</div>

Open in new window


currently my cshtml for the partialview just has this text:
This is partial view

I'm unsure how to link the partialview html with the data from the object (imageLocation, imageDate etc)

Can someone help me solve that please
Avatar of Kelvin McDaniel
Kelvin McDaniel
Flag of United States of America image

Update your HomeController method as follows:
[HttpPost]
public PartialViewResult CameraImage(int myId)
{
	var requestedImageData = processData.LoadGraphImageData(myId);
		
	return PartialView("~/Views/Home/CameraImage.cshtml", requestedImageData);
}

Open in new window


… then you'll be able to access the data in requestedImageData like you would with a normal Razor view.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.