0

I am using Html.RenderAction in my View to call a method that is in my controller. The controller method returns a custom object, I want to know how would I be able to use this returned object in the view.

View

//at the top
@model ServiceCheckerUI.Models.DeleteDeliverableModel

@{ Html.RenderAction("retrieveDeliverableInfo","DeliverableManagement", new {uniqueId = element});}

//Trying to use the model property
Model.deliverableResponse.{properties}

Controller

public ActionResult retrieveDeliverableInfo(string uniqueId){
    var response = _target.DoSomething();
    return PartialView("DeleteDeliverable", new DeleteDeliverableModel {deliverableResponse = response});
}

Model

namespace ServiceCheckerUI.Models
{
    public DeleteDeliverableModel
    {
        //omit
        public GetDeliverableResponse deliverableResponse {get;set}
    }
}

The GetDeliverableResponse object has fields like id, name etc which are strings and ints.

TheProgrammer
  • 1,314
  • 5
  • 22
  • 44

1 Answers1

1

RenderAction is used to directly write response to the page and helps in caching the partial view. Your method should return partial view instead of GetDeliverableResponse. You can define the partial view and use GetDeliverableResponse as it's model.

public ActionResult RetrieveDeliverableInfo(string uniqueId)
{
    var response = _target.DoSomething();
    return PartialView("_Deliverable", response );
}

Here _Derliverable would be your partial view which will have GetDeliverableResponse as model. To keep it more neat you can also Wrap the response object in a dedicated model class of _Derliverable like this:

class DerliverableModel
{
   public GetDeliverableResponse Derliverables { get; set; } 
}

now in your action method you need to pass the object of this model:

return PartialView("_Deliverable", new DerliverableModel { Derliveries = response });
vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • Thank you, I will try to begin to implement and let you know if there are any issues – TheProgrammer Oct 14 '15 at 20:29
  • Ok I have set up the partial view. How would I go by using this model that I returned in the Partial View? – TheProgrammer Oct 14 '15 at 21:55
  • My main goal is to use the GetResponseObject in the view and fill up a table with the fields in the object\ – TheProgrammer Oct 14 '15 at 21:58
  • Just like any other normal view. Create a razor view named _Deliverable use @model as GetDeliverableResponse. And do what ever you want to do by acessing Model property on the page. – vendettamit Oct 14 '15 at 21:59
  • See updated code above. I am trying to use the Model property but none of my created fields are showing up. Anything i am doing wrong? – TheProgrammer Oct 14 '15 at 22:17
  • The name of the view is DeleteDeliverable – TheProgrammer Oct 14 '15 at 22:20
  • Which view? You mean the main view? – vendettamit Oct 14 '15 at 22:21
  • Sorry the main view is called DeliverableManagement and the partial View is DeleteDeliverable – TheProgrammer Oct 14 '15 at 22:22
  • Ok then model on DeleteDeliverable should be the one you created and use tha view name in RenderAction method and you should be good to go. – vendettamit Oct 14 '15 at 22:24
  • Sorry I am very new to this, I am able to use Model.deliverableResponse however when I go underneath deliverableResponse the only thing that shows is ExtensionData, ToString(), Equals etc. How would I use the viewName in the RenderAction? The second parameter in the RenderAction is the name of the folder that the View is located in. – TheProgrammer Oct 14 '15 at 22:30
  • You have to write it as string like written in action method simply write RenderAction("DeleteDeliverable",....) keep the rest as is. this should work. – vendettamit Oct 14 '15 at 22:33
  • Donot use render action on Delete partial view. Render action method is to show a view like control on mail view. – vendettamit Oct 14 '15 at 22:35