Partial Views on mvc create view that use a dropdown list to populate the partial view's view bag is this possible in mvc?

╄→гoц情女王★ 提交于 2019-12-29 02:01:18

问题


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?

Update:

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.

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.

Can a Drop Down List Trigger A Partial View To Update on A Create View Form In mvc?

Null view bag and partial view

Populating Partial Views using mvc

Updating a Partial View in MVC 5


回答1:


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

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

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();
    }

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);


    }

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.

This is in the [HttpPost]

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

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>

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();
                    }
                });

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!




回答2:


No, partial views do not necessarily have to be strongly typed, if that's your question. You can have a partial view with just html markup.



来源:https://stackoverflow.com/questions/41988881/partial-views-on-mvc-create-view-that-use-a-dropdown-list-to-populate-the-partia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!