mvc上的部分视图会创建一个使用下拉列表填充部分视图的视图包的视图,这在mvc中可能吗? [英] Partial Views on mvc create view that use a dropdown list to populate the partial view's view bag is this possible in mvc?

查看:83
本文介绍了mvc上的部分视图会创建一个使用下拉列表填充部分视图的视图包的视图,这在mvc中可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mvc上的Partial Views是否可以创建一个使用下拉列表的视图,该视图将下拉列表中的值发送到基于该下拉列表值选择创建一个列表的函数中,然后将其存储在用于部分视图的视图包中可以在mvc中完成,也可以在创建mvc表单的视图时完成吗?

can a Partial Views on mvc create view that is using a dropdown list that sends value from the dropdown list to a function that creates a list based on the dropdown list value selection, That is then stored in a view bag for the partial view.. Can this be done in mvc and can it be done on create view of a mvc form?

我可以看到它在编辑视图中如何工作,因为在页面加载时已经选择了下拉列表值.

I can see how something this would work in the edit view because the dropdown list value has already been selected when the page loads.

但是在新的创建"视图记录上,没有选择任何内容,因此列表函数具有空值

But on a new Create view record nothing is selected so the list function has a null value

部分视图是否仅适用于预先填充了数据的表单?

Are partial views only for forms that have data pre-populated in them?

更新:

我有一个由Visual Studio向导创建的创建视图.它既有帖子,又有创建者.用户在创建视图时.我在页面表单上有一个下拉列表以及其他字段,但是在该新创建页面的加载中它为空.对我来说不幸的是,我希望我的局部视图填充一个数据列表,该数据列表在用户从下拉列表中进行选择之后发送到视图包.

I have a create view that was created by the visual studio wizard. It has both a post and get under the create. When the user in the create view. I have a dropdown list on the page form with other fields but on load of that new create page it is empty. Unfortunately for me I wanted my partial view to to get populated with a list of data that gets sent to a view bag after the user make a selection from the drop down list.

我认为我要执行的操作只能使用webforms来完成,因为mvc似乎可以很好地处理动态数据.而且由于页面加载时下拉列表没有值..无法构建列表,因此如果我在下拉列表中硬编码一个值,则将出现空值错误以及列表为空.

I think what I am asking to do can only be done with webforms as mvc can handle dynamic data all that well it seems. And since when the page loads the dropdown has no value.. the list can't built so there is a null value error as well as and empty list if I hard code a value in the drop down list.

这是我在不同尝试线程中的代码,这些代码带有不同版本的文档,记录了我的多次尝试.正如我所总结的那样,这是不可能的.

Here is my Code in these different attempt threads with different veration of my code documenting my many attempts. As I have comcluded it is not possible sadly.

空视图包和局部视图

使用mvc填充局部视图

在MVC 5中更新部分视图

推荐答案

因此,在Matt Bodily的帮助下,您可以使用视图包和称为阿贾克斯这就是我使代码工作的方式.

So with help from Matt Bodily You can Populate a Partial View in the create view triggered by a changed value in a drop down list using a view bag and something called Ajax. Here is how I made my code work.

首先,您需要检查空数据的局部视图代码示例

First the partial view code sample you need to check for null data

_WidgetListPartial

_WidgetListPartial

 @if (@ViewBag.AList != null)
    {
    <table cellpadding="1" border="1">
    <tr>
        <th>
            Widget Name 
        </th>
     </tr>

@foreach (MvcProgramX.Models.LIST_FULL item in @ViewBag.AList)
   {
    <tr>
        <td>
            @item.WidgetName
        </td>        
    </tr>
   }

   </table>
  }

使用功能将View Bag填充到控制器中

Populating your View Bag in your controller with a function

    private List<DB_LIST_FULL> Get_List(int? VID)
    {

        return db.DB_LIST_FULL.Where(i => i.A_ID == VID).ToList();
    }

在您的Create控制器中,使用[HttpGet]元素添加类似的结构 这会将您的数据和部分视图发送到创建屏幕上的屏幕占位符.VID将是下拉列表中的ID,此功能还将部分视图发送回创建表单屏幕

In your Create controller add a structure like this using the [HttpGet] element this will send you data and your partial view to the screen placeholder you have on your create screen The VID will be the ID from your Drop down list this function also sends back the Partial View back to the create form screen

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

        ViewBag.AList = Get_List(VID);
        return PartialView("_WidgetListPartial",ViewBag.AList);


    }

如果需要的话,我不是100%,但是我在ActionResult中添加了以下内容:创建表单ID和FormCollection,以便可以从下拉列表中读取值.同样,如果有的话,Ajax的东西可能会很小心,但以防万一并且应用程序似乎正在使用它.

I am not 100% if this is needed but I added to the the following to the ActionResult Create the form Id and the FormCollection so that I could read the value from the drop down. Again the Ajax stuff may be taking care if it but just in case and the application seems to be working with it.

这是在[HttpPost]

This is in the [HttpPost]

   public ActionResult Create(int RES_VID, FormCollection Collection, [Bind(Include = "... other form fields

这再次位于[HttpGet]中,也可能不需要.这是从表单中读取值

This is in the [HttpGet] again this too may not be needed. This is reading a value from the form

 UpdatePartialViewList(int.Parse(Collection["RES_VID"]));

在创建视图"屏幕上,您希望在其中显示部分视图

On Your Create View Screen where you want your partial view to display

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

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

                @{ Html.RenderAction("UpdatePartialViewList");}



            </div>
        </div>

最后,后面的Ajax代码从下拉列表中读取点击.获取所选项目的值,并将值传递回后面的所有控制器代码以构建列表,并将其发送以更新部分视图,如果有数据,则将带有更新列表的部分视图传递给创建表单.

And finally the Ajax code behind that reads the click from the dropdown list. get the value of the selected item and passed the values back to all of the controller code behind to build the list and send it to update the partial view and if there is data there it pass the partial view with the update list to the create form.

    $(document).ready(function () {
        $('#RES_VID').change(function ()
        {

            debugger;

            $.ajax(

                {
                    url: '@Url.Action("UpdatePartialViewList")',
                    type: 'GET',
                    data: { VID: $('#RES_VID').val() },

                    success: function (partialView)
                    {
                        $('#PV_WidgetList').html(partialView);
                        $('#PV_WidgetList').show();
                    }
                });

很多方法不是最好的方法,但这是一个行之有效的经过测试的答案,它是该过程的每一步,希望其他人不必经历我必须经历的为期多天的恐怖表演经历了一些基于错误的工作,最初我认为该错误无法在mvc中完成,而我将不得不继续以webforms的形式运行该应用程序.再次感谢帮助我制定此解决方案的每个人!

This many not be the best way to do it but this a a complete an tested answer as it work and it is every step of the process in hopes that no one else has to go through the multi-day horror show I had to go through to get something that worked as initially based on the errors I thought this could not be done in mvc and I would have to continue the app in webforms instead. Thanks again to everyone that helped me formulate this solution!

这篇关于mvc上的部分视图会创建一个使用下拉列表填充部分视图的视图包的视图,这在mvc中可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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