使用 mvc 填充部分视图 [英] Populating Partial Views using mvc

查看:46
本文介绍了使用 mvc 填充部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次问候各位.首先,我阅读了 20 多篇关于此主题的帖子和至少 15 个非股权交换链接,并就此问了我自己的 5 个问题.

首先我只想知道 mvc 可以在创建视图中使用动态数据填充局部视图吗?如果不是,那么我怀疑的是正确的.这只能在 webforms 中关闭,我可以删除这个 mvc 应用程序并只使用 webforms.

如果可以的话,请展示如何做到这一点.再次这里是我所拥有的一切.

声明部分视图代码调用

_PartVue 存放在mvc 代码文件夹后面的共享文件夹中此零件视图是使用向导构建的

<tr><th>字段 1</th><th>场 2</th><th>字段 3</th></tr>@foreach(@ViewBag.List 中的 System.Models.DATA_LIST 项目){<tr><td>@item.F1</td><td>@item.F2</td><td>@item.F3</td></tr>}

在创建视图中

这是我希望局部视图显示在创建表单上的表单中的位置

 

<div class="form-horizo​​ntal" style="display:none" id="PV_List">@{ Html.RenderAction("ShowList",);}

创建视图中的java脚本代码,用于在下拉视图更改后显示的列表

 $(document).ready(function () {$('#RES_ID').change(function (){调试器;$.get('~/Views/Shared/_PartVue.cshtml', { VID: $(this).val() }, function (data) { $('#PV_List').html(data); });$("#PV_List").show();//显示局部视图});}

在控制器中,我有一个创建列表的函数和一个使用 return PartialView 的函数:

 [HttpGet]公共 ActionResult ShowList(int?VID){ViewBag.DataList = Get_List(VID);return PartialView("~/Views/Shared/_PartVue.cshtml",ViewBag.DataList);}

还有填充视图包数据的Get List函数

 私有列表GET_List(int?VID){返回 db.LIST_DATA.Where(i => i.ID == VID).ToList();}

我在创建时得到的是字段,但没有数据.当进入后面的代码时,我看到选择了正确的数据,但它没有进入局部视图表单.

这是我得到的.

在谷歌浏览器中,我收到以下错误:http://localhost:50296/PrograX/~/Views/Shared/_PartVue.cshtml?VID=808 无法加载资源:服务器响应状态为 403(禁止)

这个局部视图在共享文件夹中...所以 mvc 告诉我它不能用动态数据填充局部视图,我需要使用 webforms 而不是尝试使用 mvc?

再次非常感谢.希望这个问题能更好地解释我想要做什么,也许我可以获得更好的指导,让我知道这是不可能的,也不能在这个平台上完成.谢谢

解决方案

首先,您可以指定要在局部视图中使用的模型,如下所示:

@model List 

那么你可以在 foreach 中使用它,如下所示:

@if(Model.Count > 0){foreach(模型中的变量项目){<tr><td>@item.F1</td><td>@item.F2</td><td>@item.F3</td></tr>}}

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

解决方案

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>
   }
}

这篇关于使用 mvc 填充部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆