Updating a Partial View in MVC 5

2019-09-07 15:34发布

I am getting an error when trying to load a partial view that should display a list on the create view of the MVC app. The list is based on a value will come from a list of values drop control.

On create view there is no selection so the list is empty and will need to refreshed after the user selects a value while in the MVC create view.

I followed the accepted answer on this question and got errors:

Updating PartialView mvc 4

But I have some questions about what is being said.

Someone said: "There are some ways to do it. For example you may use jQuery:" and he shows the Java query.

But he also shows another method and says: "If you use logic in your action UpdatePoints() to update points"

[HttpPost]
public ActionResult UpdatePoints()
{    
   ViewBag.points =  _Repository.Points;
   return PartialView("UpdatePoints");
 }

I get the following error

The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult UpdateList(Int32)' in 'System.Controllers.RController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

I have no clue what this error means

So in create view:

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

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

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

            </div>
        </div>

In controller under the create action as its own function

    [HttpGet]
    public ActionResult UpdateList(int ID)
    {

        if (ID != 0)
        {
            ViewBag.List = Get_List(ID);
            return PartialView("PV_List");

        }
        else
        {
            ViewBag.List = "";
            return PartialView("");
        }


    }

And the function that makes the list for the view bag function:

private List<SQL_VIEW_LIST> Get_List(int ID)
        {

            return db.SQL_VIEW_LIST.Where(i => i.ID == ID).ToList();
        }

The JavaScript for the for the list of values drop down list of values: That also controls turning on the visibility of the list when it has data:

    //Fire The List to make visible after list values select 
    $(document).ready(function () {
        $('#RES_VEH_ID').change(function ()
        {

            $("#PV_List").show(); // Shows Edit Message
            $.post('@Url.Action("PostActionTo_Partial","Create")').always(function()
                   { ('.target').load('/Create'); })

        });
    })

Also does anyone know what this string mean: ? "PostActionTo_Partial"

Also does anyone know what this means ViewBag.points = _Repository.Points; I get the view bag part but it's the _Repository.Points; part that I don't understand. Any one have any ideas of what is going on there?

2条回答
混吃等死
2楼-- · 2019-09-07 16:10

I can't understand what do you try to do. But i'll try to answer.

I have no clue what this error means.

This error means that model binder can't find parameter "ID" for action method

public ActionResult UpdateList(int ID)

Because you don't send any parameter for this method: You can try this:

@{ Html.RenderAction("UpdateList", new {ID="value"});}

Or you can set default value in your method:

public ActionResult UpdateList(int ID=value)

or make "ID" nullable:

public ActionResult UpdateList(int? ID)

Also does anyone know what this string mean: ? "PostActionTo_Partial"

this is "action name" in yor controller

Also does anyone know what this means ViewBag.points = _Repository.Points;

it means assigning dynamic object "VivBag.points' data to transfer them into view

查看更多
乱世女痞
3楼-- · 2019-09-07 16:22

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!

查看更多
登录 后发表回答