Null view bag and partial view

房东的猫 提交于 2019-12-25 09:07:46

问题


I am trying to use partial views but I am completely lost. Originally I was trying to pass dynamic data to the partial view but I ended up getting a 403 forbidden when you press the google browser F12 key.

Partial View that I have reduced to one field thinking that would allow it to load data

      <table cellpadding="1" border="1">
          <tr>
            <th>
                FIELD LBL1  
            </th>          
          </tr>

        @foreach (MvcProgram.Models.LIST_FULL item in @ViewBag.ListFull)
        {
            <tr>
                <td>
                    @item.FIELD1_DATA
               </td>
            </tr>
    }

    </table>

The error which indicates to me that mvc cannot push dynamic data using a view bag as it said forbidden. So I am scrapping that idea as I have tried all sorts of workarounds that fail. Now I just want to populate a partial view with static data in a view bag, But I guess since there is no value to make the list when the page is on the create view that fails immediately. With this error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

And it stops on my partial view's foreach loop

       @foreach (ComputerProgram.Models.LIST_FULL item in @ViewBag.ListFull)

So can MVC use partial views at all on create view? My list is built by using an ID from the the dropdown list, but of course there in no selection on create view until form has been interacted upon by the users.

So my question is MVC capable of even rendering partial views in create view or is this something you can only do in webforms due to some mvc limitations.

The CREATE VIEW

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

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

                 @Html.Partial("_VehIssuesListPartial")

               </div>
            </div>

The only way I seem to be able to get this MVC create view to display the partial view is to build the list for the view bag and populate it with a known record of it to render. This can't be right: are partial views only usable on screen where there is data on the screen already?

The CONTROLLER CODE both the one that says get and post code:

    // GET: VRS_REQUEST/Create**********************************
    public ActionResult Create()
    {
          some code stuff

        ViewBag.VehIssuesList = GetList(808); //Force it to get something so that the page loads

        return View(model);

    }

The Post create code:

    // POST: VRS_REQUEST/Create
        [HttpPost]
        [ValidateAntiForgeryToken]


        public ActionResult Create(int RES_ID, FormCollection Collection,     [Bind(Include = Some Fields.... 

        {
            //ViewBag.ListFull = GetList(RES_ID);

            //UpdateList(int.Parse(Collection["RES_ID"])); // Was part of the attempt at dynamic data for partial view that shows forbidding


          ... CODE THAT DOES STUFF

                return RedirectToAction("Index");
            }
            return View(model);
        }

The way the current code work, it ignores the dropdown list and just populates with the value I hard coded. Some will say why I am using a view bag, well it's what I know. I don't understand models at all. If this can't be done with create views because the screen is empty then cool that work for me - I just need to know that.

Here is my code as I have aborted the idea of try to do this with dynamic data again MVC is saying it is forbidden.

If I remove this from the create controller get function, the page will not load and I get the null error;

      ViewBag.VehIssuesList = GetList(808);

I have to force a value for the page to load... so again is this because of me trying to do this on the create view and MVC can't handle that?


回答1:


_WidgetListPartial The @if statement will handle the null situation. if the data for the partial view comes back as null.

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


来源:https://stackoverflow.com/questions/41985224/null-view-bag-and-partial-view

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