简体   繁体   English

在MVC 5中更新部分视图

[英]Updating a Partial View in MVC 5

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. 尝试加载部分视图时出现错误,该视图应在MVC应用程序的创建视图上显示一个列表。 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. 在创建视图上,没有选择,因此该列表为空,并且在MVC创建视图中用户选择一个值后,将需要刷新该列表。

I followed the accepted answer on this question and got errors: 我在这个问题上遵循了公认的答案,但出现错误:

Updating PartialView mvc 4 更新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. 有人说:“有一些方法可以做到。例如,您可以使用jQuery:”,他显示了Java查询。

But he also shows another method and says: "If you use logic in your action UpdatePoints() to update points" 但是他还显示了另一种方法,并说:“如果您在操作中使用逻辑UpdatePoints()来更新点”

[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'. 参数字典包含“ System.Controllers.RController”中方法“ System.Web.Mvc.ActionResult UpdateList(Int32)”的非空类型“ System.Int32”的参数“ ID”的空条目。 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: 用于“值列表”的JavaScript会在“值列表”下拉列表中:该控件还控制在有数据时打开列表的可见性:

    //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" “ PostActionTo_Partial”

Also does anyone know what this means ViewBag.points = _Repository.Points; 还没有人知道这是什么意思ViewBag.points = _Repository.Points; I get the view bag part but it's the _Repository.Points; 我得到了视图包部分,但它是_Repository.Points;。 part that I don't understand. 我不明白的部分 Any one have any ideas of what is going on there? 有人对那里发生的事情有任何想法吗?

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 此错误意味着模型联编程序无法找到操作方法的参数“ ID”

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: 或使“ ID”为空:

public ActionResult UpdateList(int? ID)

Also does anyone know what this string mean: ? 也有人知道这个字符串是什么意思吗? "PostActionTo_Partial" “ PostActionTo_Partial”

this is "action name" in yor controller 这是您控制器中的“动作名称”

Also does anyone know what this means ViewBag.points = _Repository.Points; 还没有人知道这是什么意思ViewBag.points = _Repository.Points;

it means assigning dynamic object "VivBag.points' data to transfer them into view 这意味着分配动态对象“ VivBag.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 因此,在Matt Bodily的帮助下,您可以使用视图在下拉列表中更改值触发的创建视图中填充部分视图

bag and something called Ajax. 包和一个叫做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>
  }

Populating your View Bag in your controller with a function 使用功能将View Bag填充到控制器中

    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 在[创建]控制器中,使用[HttpGet]元素添加一个类似的结构,这将向您发送数据和部分视图到创建屏幕上的屏幕占位符。VID将是Drop的ID。

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 如果需要这样做,我不是100%,但是我将以下内容添加到了ActionResult中:创建表单ID和FormCollection,以便

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 同样,如果有的话,Ajax的东西可能会很小心,但是以防万一并且应用程序似乎可以

it. 它。

This is in the [HttpPost] 这是在[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. 再次在[HttpGet]中也可能不需要。 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. 最后,后面的Ajax代码从下拉列表中读取点击。 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 aa 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. 这在mvc中无法完成,而我将不得不在webforms中继续该应用程序。 Thanks again to everyone that helped me formulate this 再次感谢所有帮助我制定此原则的人

solution! 解!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM