Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am tring to show some tables from db selected by company id
on ddl change I a jquery post to the controller that rendering a partial view and allthough, the data is entering the loops the html is not rendered.
when I put a number in the partilal view directly I get the tables html.

this is the 'event':

JavaScript
$("#CompniesList").on('change', function () {
     var id = $("#CompniesList option:selected").val();
     var jsonData = { companyId: id };


     $.post('/CompanyReceapts/GetReceapts/',
          jsonData,
          function (response) {
              if (parseInt(id) > 0) {
                  $('.tblReceapt').show();
              }}
          );

     });

this is the controller:
C#
[HttpPost]
    public PartialViewResult GetReceapts(string companyId)
    {
        int id;
        int.TryParse(companyId, out id);
        var companies = new ReceaptsApp.Models.ReceaptModel.Companies();

    //    ViewData["CompanyList"] = new SelectList(companies.CompaniesList, "Value", "Text");

     //   ViewBag.CompanyId = id;
        var receaptListCompany = new ReceaptsListForCompany_VM
        {
            CompanyName = companies.CompaniesList.SingleOrDefault(x => x.Value == id.ToString()).Text,
            CompanyId = id,
        };
        receaptListCompany.Receapts = receaptListCompany.GetReceaptsForCompany(id);
        return PartialView("ViewReceapt", receaptListCompany);
    }

and this is the partial view
HTML
@using ReceaptsApp.ViewModels
@model ReceaptsApp.ViewModels.ReceaptsListForCompany_VM
<script src="../../Scripts/angular.min.js"></script>

@{
    int id = Model.CompanyId;
    List<CompanyReceapt> receaptList = Model.Receapts;//this equals to //Model.GetReceaptsForCompany(id)
//  List<CompanyReceapt> receaptList = Model.GetReceaptsForCompany(1); //when I try //this I do get the tables
 
    }

@if (receaptList != null) {
    foreach (var receapt in Model.Receapts)
    {
         <table class="tblReceapt" style="display: block">
             <tr>
                 <td>Receapt No.</td>
                 <td>@receapt.ReceaptId</td>
                 <td></td>
                 <td>Date</td>
                 <td>@receapt.Date</td>
             </tr>
             <tr>
                 <td>Job </td>
                 <td>hours</td>
                 <td>hourly price</td>
                 <td>comments</td>
                 <td>sum</td>
             </tr>

             @foreach (var item in receapt.Content) {
                 <tr>
                     <td>@item.JobEssence</td>
                     <td>@item.Hours</td>
                     <td>@item.HuorlyPrice</td>
                     <td>@item.Comments</td>
                     <td>@(item.Hours*item.HuorlyPrice)</td>
                     <td></td>
                 </tr>
             }
    
         </table>
     }
}


What I have tried:

I tryed viewbag instead of model but got the same result.
thank you all in advance
Posted
Updated 16-Mar-16 4:01am
Comments
Afzaal Ahmad Zeeshan 16-Mar-16 4:43am    
HTTP POST verb is used to submit something to the server. To get something you should use HTTP GET. That is how verbs are intuitively used.

1 solution

You're simply making a post to a url and doing nothing with the response. Where are you expecting your partial view to appear and why? You have to inject the response somewhere on your page.

HTML
<select id="CompniesList">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

<div id="results">

</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#CompniesList").on('change', function () {
        var id = $(this).val();
        var jsonData = { companyId: id };

        $.post('/CompanyReceapts/GetReceapts/',
             jsonData,
             function (response) {
                 $("#results").html(response);
                 if (parseInt(id) > 0) {
                     $('.tblReceapt').show();
                 }}
             );
        });
    });
</script>
 
Share this answer
 
Comments
finishe 16-Mar-16 10:04am    
Yes! that was the problem.
Thank you F-ES Sitecore

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900