MVC4: Object reference not set as an instance of an object

0

I am currently using MVC4, and from a layout I am calling a PartialView :

@Html.Partial("~/Views/PartialView/EditForm.cshtml")

And when I run the application for the first time I get this error:

With the application URL in the following way:

localhost:50234

Then, I run the application again and it works without errors of null . But change the URL:

http://localhost:50234/PartialView/EditForm

The elements that generate conflicts are elements defined in viewmodel as "catalogos"

    public IEnumerable<String> Catalogo1  { get; set; }
    public IEnumerable<String> Catalogo2  { get; set; }
    public IEnumerable<String> Catalogo3  { get; set; }
    public IEnumerable<String> Catalogo4  { get; set; }

My question is:

  • The error is found in the call to PartialView (considering it is a viewmodel )? Should I make the call in another way?
  • The catalog elements created in viewmodel should go with "get" "set" or should they be initialized in another way?
  • Is there a way to go directly to the URL of ActionResult on the first run?
asked by seojedaperez 23.05.2017 в 15:44
source

2 answers

0

I was wrong in what I wrote before ... after about 30 executions he gave me the error again ... I spent another few hours investigating and it was the class "RouteConfig.cs" that had other values set. I left it configured like that and it does not puncture me anymore. Note that the names in English are names that were already defined by the system, just change the name of the controller and the action.

        routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "NombredeMiControlador", action = "NombreDeMiActionResult", id = UrlParameter.Optional }
    
answered by 26.05.2017 / 08:25
source
0

I already solved it, basically the problem resided in the @Html.BeginForm of the partialView view:

when using @Html.Partial("ruta del partial view")

tried to call the view with catalogs and etc because it was the only @Html.BeginForm defined.

Now what I did is an overload of @Html.BeginForm :

I put this without parameters for when you call it for the first time:

@using (Html.BeginForm())
{

}

and below I put the @Html.BeginForm that I really need:

@using (Html.BeginForm("MetodoPost", "Controllador", FormMethod.Post))

{

...

llamadas a catalogos y demas elementos Helpers Html y etc.

...

}

then the layout tries to enter the view, it finds the BeginForm() without values, then it goes to ActionResult of my Controller and it loads all the data of BeginForm() with catalogs

I hope someone will answer:)

    
answered by 24.05.2017 в 21:42