Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

asp.net 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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...