none
Ayuda, El valor no puede ser nulo. Nombre del parámetro: items RRS feed

  • Pregunta

  • Estoy realizando una aplicación en Visual Studio 2019 en ASP.nET core con mvc, pero al momento de utilizar un ViewBag me aparece el error de que el valor no puede ser nulo, así tengo mi código...

    <div class="form-group" style="margin-bottom:15px;">
            @Html.Label("Opciones de la votación", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">                    
                 @Html.DropDownListFor(model => model.OptionVoting, new SelectList(ViewBag.Vote, "Voting_Id", "OptionVoting"), new { @class = "form-control", @required = "required" })
                 @Html.ValidationMessageFor(model => model.Voting_Status, "", new { @class = "text-danger" })
            </div>
    </div>

    Así mi controlador...

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult EditVoting(int? id, [Bind(Include = "Voting_Title,Voting_Description,OptionVoting")] Voting voting)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        Voting vote = db.Voting.Find(id);
                        vote.Voting_Title = voting.Voting_Title;
                        vote.Voting_Description = voting.Voting_Description;
                        vote.OptionVoting = voting.OptionVoting;
                        db.SaveChanges();
                        TempData["Success"] = "Se ha modificado el voto con éxito";
                        return RedirectToAction("/Voting/1");
                    }
                    TempData["Error"] = "Verifica el formulario de modificación";
                    ViewBag.Vote = db.Voting.Where(x => x.Voting_Status == "Active");
                    return PartialView(voting);
                }
                catch (Exception ex)
                {
                    TempData["Error"] = ex.Message;
                    ViewBag.Vote = db.Voting.Where(x => x.Voting_Status == "Active");
                    return PartialView(voting);
    
                }
            }

    y este es el seguimiento de pila...

    [ArgumentNullException: El valor no puede ser nulo.
    Nombre del parámetro: items]
       System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, String dataGroupField, IEnumerable selectedValues, IEnumerable disabledValues, IEnumerable disabledGroups) +169
       CallSite.Target(Closure , CallSite , Type , Object , String , String ) +191
       System.Dynamic.UpdateDelegates.UpdateAndExecute4(CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3) +758
       ASP._Page_Views_Association_EditVoting_cshtml.Execute() in C:\Users\user\Documents\jausproject\Web\NeighborAssociation\NeighborAssociation\Views\Association\EditVoting.cshtml:78
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +90
       System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +256
       System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
       System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +277
       System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +91
       ASP._Page_Views_Association_Voting_cshtml.Execute() in C:\Users\user\Documents\jausproject\Web\NeighborAssociation\NeighborAssociation\Views\Association\Voting.cshtml:131
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
       System.Web.WebPages.StartPage.RunPage() +17
       System.Web.WebPages.StartPage.ExecutePageHierarchy() +64
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
       System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +256
       System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
       System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
       System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
       System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
       System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
       System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
       System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
       System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9836613
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163

    sábado, 22 de febrero de 2020 22:23

Respuestas

  • Hola. Al aplicar un redireccionamiento ViewBag umno se encuentra disponible. Emplea para ello TempData ya que ll tienes disponible en todo el flujo

    Si se solucionó tu consulta no olvides marcar la respuesta. Si te ayudó, vótala como útil. Saludos

    domingo, 23 de febrero de 2020 14:40
    Moderador