0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    this sounds to me like you need to load a partial view with an ajax call. I googled "mvc partial view ajax" and 2 good results are http://stackoverflow.com/questions/32610270/how-to-render-partial-view-in-mvc5-via-ajax-call-to-a-controller-and-return-html and http://stackoverflow.com/questions/10589787/asp-net-mvc-returning-a-partialview-to-ajax-along-with-another-object – Matt Bodily Feb 01 '17 at 21:01
  • @MattBodily Hi Matt yes some one mentioned ajax... as I don't really know what ajax or how to use it. I did not want to add 3rd party addons to the application. I am trying to use what I know and that is a view bag for sending data and the concept of partial views. As they related closely to what I know which are these things called usercontrols. Using user controls something like this would be very easy. but not so much in mvc. –  Feb 01 '17 at 21:13
  • 1
    I would strongly recommend that you look into jquery and ajax. the links that I provided show exactly how to set it up. They are powerful tools and will make your life much easier when working with MVC. just my 2 cents :) – Matt Bodily Feb 01 '17 at 21:18
  • @MattBodily yes sir.. reading it now but it is not addressing my primary problem which is that My create view has no data initially.. my data that my partial view uses uses as its parameter for the data that needs to be displayed the value from the dropdown list. so the page will not load because there is no data from the partial view to display. I can see that no one has tried to do what I am trying to do which is why I think mvc may not have this functionality because it may not be able can't render an empty partial view which in my case it will need to because it loading on an empty form –  Feb 01 '17 at 21:30
  • sounds like you are looking for something like a cascading drop down. where you load values based on what is selected in the drop down. https://www.mindstick.com/Articles/1482/cascading-dropdownlist-using-ajax-in-asp-dot-net-mvc-4 this is an example of doing that again using ajax – Matt Bodily Feb 01 '17 at 21:36
  • @MattBodily no sir I am not... I just want a partial view to load a list of data that comes from a view bag when the user clicks on the dropdown list. Not cascading if I understand the definition of what cascade means in mvc as it may have a different meaning. Picture if you will the user clicks create and a form opens on screen with i field and a dropdown list. and in a hidden div there is an area from a partial view. The user types in their name can clicks the drop down and selects widget A from the dropdownlist. when they click and select widget A the partial view loads a list of widget As –  Feb 01 '17 at 21:42
  • @MattBodily that is what I am trying do populate a partial view based on what the user selects in a drop list. One of My problem is when the form loads there are is no clected value for the sql parameter so the page does not render because the list is empty. the other problem is that I am using a view bag and no one seems to like view bags and keep trying to say use a model and I don't understand models in that context because My page already has a model I understand view bags much better. So it's been a real horror show! –  Feb 01 '17 at 21:45
  • 1
    you can load a drop down more directly like this http://stackoverflow.com/questions/18963618/how-to-populate-an-dropdownlistfor-from-database-in-asp-net-mvc-4/18965261#18965261 . for loading the partial as you described, that is exactly what ajax is for. the cascading link will show how to fire the ajax call from the drop down event – Matt Bodily Feb 01 '17 at 21:47
  • 1
    the look at the first 2 links on how to take that data on the server and return a partial view that you can display – Matt Bodily Feb 01 '17 at 21:48
  • @MattBodily Ok thanks so much I will study those thanks again! Looking at your drop down structure I do the same way.. but if it is null what should be returned so that the partial view does not fail because there is no data? –  Feb 01 '17 at 21:52
  • @MattBodily Wow Matt I owe you so many thanks... you were spot on with the Ajax thing... it WORKS!! Thank so much I will add the Answer details.. I so wish I could give you a thumbs up sir!! Thanks again! –  Feb 01 '17 at 22:46
  • 1
    glad you got it working, anytime :) – Matt Bodily Feb 02 '17 at 15:37

2 Answers2

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!

0

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.

Federico Alecci
  • 914
  • 6
  • 14
  • I don't thank that is what I am asking...I will ahve to look up the meaning of strongly typed. I have added more details to my question.. I just wanted a partial view to work on the create form but the data from the create from is based on a dropdownlist selection that is on the create view which as no value as this create view is adding a new record to the database. –  Feb 01 '17 at 21:16