Populating Partial Views using mvc [duplicate]

greeting again guys. First I have read over 20 post on this topic and at least 15 none stake exchange links as well as asked 5 question of my own regarding this.

First I just want to know can mvc populate a partial view with dynamic data while in the create view? If not then what I suspected was correct. This can only be down in webforms and I can drop this mvc app and just use webforms.

If it can can some one please show how to do this. Again here is everything that I have.

stating with the partial view code called

_PartVue that is stored in the shared folder of the mvc code behind folders this part view was build using the wizard

<table cellpadding="1" border="1">
    <tr>
        <th>
            Field1
        </th>
        <th>
            Field2
        </th>
        <th>
            Field3
        </th>

   </tr>

    @foreach (System.Models.DATA_LIST item in @ViewBag.List)
    {
        <tr>
            <td>
                @item.F1
            </td>
            <td>
                @item.F2
            </td>
            <td>
                @item.F3
            </td>
        </tr>
    }

</table>

In the Create View

This is where in the form that I want the partial view to show up on the create form

        <div class="col-sm-6">

            <div class="form-horizontal" style="display:none" id="PV_List">

                @{ Html.RenderAction("ShowList",);}


            </div>
        </div>

The java script code in the create view for the list to show after a drop down view is changed

    $(document).ready(function () {
        $('#RES_ID').change(function ()
        {
            debugger;

            $.get('~/Views/Shared/_PartVue.cshtml', { VID: $(this).val() }, function (data) { $('#PV_List').html(data); });
            $("#PV_List").show(); // Shows Partial View


        });
    }

in the controller I have a function that makes a list and a function that uses something called return PartialView:

    [HttpGet]
    public ActionResult ShowList(int? VID)
    {


        ViewBag.DataList = Get_List(VID);
        return         PartialView("~/Views/Shared/_PartVue.cshtml",ViewBag.DataList);


    }

Also the Get List function that populates the view bag data

    private List<LIST_DATA> GET_List(int? VID)
    {

        return db.LIST_DATA.Where(i => i.ID == VID).ToList();
    }

What I get on the create from are the fields but with no data. WHen is step into the code behhind I see that the right data is selected but it does not go no the partial view form.

Here is what I get.

In the google browser I get the following error: http://localhost:50296/PrograX/~/Views/Shared/_PartVue.cshtml?VID=808 Failed to load resource: the server responded with a status of 403 (Forbidden)

This partial view is in the shared folder... so is mvc telling me thatit can't populate a partial view with dynamic data and that I need to use webforms instead of trying to use mvc?

thanks so much again. Hopefully this question better explain what it is I am trying to do and maybe I can get better guidance letting me know that it is not possible and that it can't be done in this platform. Thanks

Answers:

Answer

First of all, You can specify which model you want to use in your partial view like this :

@model List <YourSolutionName.YourFolderNameWhereModelIs.YourModelName>

then you can use it in foreach as follows:

@if(Model.Count > 0)
{
 foreach(var item in Model)
 {
        <tr>
            <td>
                @item.F1
            </td>
            <td>
                @item.F2
            </td>
            <td>
                @item.F3
            </td>
        </tr>
   }
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.