Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a ViewModel for a partial view in a view

I am curious how to do the following. I have a main view with parent data about support cases. I also will be showing a child list of MTM data using a partial view. I have built a ViewModel to supply the partial view with the data needed.

Here is my ViewModel:

public class CaseComplaintsViewModel
    {
    public string ComplaintCode { get; set; }
    public string ComplaintType { get; set; }
    }

The ViewModel is based on data from these two domain models:

public class CaseComplaint
    {
    [Key]
    public int CaseComplaintID { get; set; }
    public int CasesID { get; set; }
    public int ComplaintCodeID { get; set; }
    public virtual Cases Cases { get; set; }
    public virtual ComplaintCode ComplaintCode { get; set; }
    }

public class ComplaintCode
        {
        public int ComplaintCodeID { get; set; }

        [MaxLength(50)]
        [Required(ErrorMessage="Complaint Code is required")]
        public string ComplaintCodeName { get; set; }

        [MaxLength(50)]
        [Required(ErrorMessage="Complaint Type is required")]
        public string ComplaintType { get; set; }

        public virtual ICollection<CaseComplaint> CaseComplaint { get; set; }

        }

Pretty simple.

Here is the partial view.

@model IEnumerable<cummins_db.ViewModels.CaseComplaintsViewModel>

<table width="100%">
<tr>
<th>Complaint Code</th>
<th>Complaint Description</th>
</tr>  
@foreach (var item in Model)
    {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.ComplaintCode )    
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ComplaintType)
    </td>

</tr>
    }
</table>

This partial view will be used inside of my main Cases view. I will also periodically refresh this partial view using ajax.

So my questions is, how do I get the correct data into the view when my main cases view opens? Does the partial view need it's own controller or do I handle that in the Cases controller?

like image 687
Ryan Avatar asked Sep 20 '25 02:09

Ryan


1 Answers

Yes, it needs his own Action were you handle the data send it to the client. For example

public ActionResult ShowCaseComplaints(int someID)
{
    //creates a linq or database sql could something like
    /*var data = db.Complains.Find(someID).Select( x => new CaseComplaintsViewModel(){
        ComplaintCode = x.ComplaintCode,
        ComplaintType = x.ComplaintType
    }).ToList();*/

    var data = (from C in db.CaseComplaint 
                where C.CaseID == someID
                select C.ComplaintCode).ToList().Select( x => new CaseComplaintsViewModel(){
                     ComplaintCode = x.ComplaintCode,
                     ComplaintType = x.ComplaintType
                }).ToList();
    return PartialView("Your_Partial_View_Name", data);
}

Notes that i'm assuming you have a base class from where you are creating the ViewModel.

To render the partial from your main view you just called in main view like this assuming that you main view it's strongly typed for

@model CaseComplaint

<div id="complaintlist">
    @Html.Action("ShowCaseComplaints","yourController", new { someID = model.CaseID}
</div>
like image 82
Jorge Avatar answered Sep 22 '25 21:09

Jorge