last day (14 days later) » 

8:21 AM
1
A: Using Ajax to render partial view, taking an object as parameter

KatstevensI think what you are seeking is some HTML back from your AJAX call that you can then render inside an existing element. Is that correct? If so, the easiest way is to construct a view that contains the correct model, and return it from your method. So you would write up a view that contains only ...

 
Thank you! This is what im trying to achieve: In my index.cshtml I have a canvas on whch I can draw a picture (like paint). Then, when I press a button, the "Art" gets saved to the database in form of an href to an imagehost-site- This Parts works fine. But what try to do is to pass the newly created "art" to a partial view.
 
OK the code example will work for you then. In your new view (which I called TestView you would set the src attribute of the image to the link returned from the hosting site - this will then display the image when the returned HTML is rendered. I'll update the example.
 
Thank you very much! Im sure this is the right way to go about It but when I step through the code, it does not reach the sucess-mehod, everthings works fine until this line in the _layout(where I have the ajax): @RenderSection("scripts", required: false) The it "jumps out". Should I maybe place the Ajax elsewhere?
 
Are you saying that the error occurs in the _Layout page when your AJAX method runs?
 
It goes like this: The object gets created and returns this: return View("TestView", nyArt); This works fine and i can step through the "TestView" and see that the href is where it should be. Next the code goes to the _layout but it never reaches the AJAX this time. After this line: @RenderSection("scripts", required: false) The code gets out of the _Layout and im back on my page wth nothing new rendered.
 
8:21 AM
You probably have a default layout page set in the ViewStart page. Check my edit in the view code to set the layout page to null.
 
8:31 AM
Hello Katstevens, are you there?
 
Yes I'm here - what's up?
 
Hi there! Thanks a lot for taking the time to help!
Did you perhaps see the edit on my question?
 
No worries :)
In your view (TestView) remove all the <HTML><head><body> stuff, as you only want to return the exact HTML you need for the page
Also make sure you have
@{
Layout = null
}
at the top of your view, just under @Model - this will remove any layout on the view
 
So this should be it:
@model Heroz.Models.ArtWork
<img src="@Model.ArtLink" />
 
Yes, but like this:
@model Heroz.Models.ArtWork
@{
Layout = null;
}
<img src="@Model.ArtLink" />
The reason is you will have a _ViewStart file in your Views folder that specifies a default layout for all views - so you need to explicitly set the Layout to null to override that
So this will return ONLY an <img> tag back to your AJAX success handler, which you can then do with what you want.
Alternatively, you could also just return the link text as a string by changing the return type of the action method:

public string MyAction()
{

}
 
8:41 AM
Ok, i will now try to step through it and see...hang on..
 
then you would return the ArtLink as a string to the AJAX method:

return nyArt.ArtLink;
Hey, I'm sorry - I have to go out (dinner!). Let me know how it goes, if it doesn't work I'll check this thread when I get back and give you some other options to try. Sorry to bail out so soon! Good luck :)
 
this time the code steps into the testview but no further...I imagine that at some point it should return to the AJAX and ..
Ok THanks a lot for your time!
Have a good dinner! See you later!
 
 
3 hours later…
11:53 AM
Hey, still around?
 

last day (14 days later) »