Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
- I had to convert the method in the controller from HttpGet to HttpPost.

- The method returns a view to me.

- I have previously sent values ​​to this method via ViewBag.

- Currently, when I use ViewBag, an error is thrown:

>Failed to load resource: the server responded with a status of 500 (Internal Server Error)

- The error only pops up when I'm using ViewBag


controller:
[HttpPost]
public ActionResult PartialTabelaEcp(string json)
{ 
    var nr_days= 31;
    ViewBag.days= nr_days;
    return PartialView("_TableEve");
}


.cshtml
//for example
@for (int nr_rows = 0; nr_rows < ViewBag.days; nr_rows++)
{
    var nr_names = nr_rows + 1;
}



what I expected?

Help / articles on how to send data from the controller to the view using HttpPost

What I have tried:

for example:
model:

public partial class userDaty_Model
   {
       public int liczbaDniM { get; set; }

       public int numerMiesiacaM { get; set; }

       public int numerRokuM { get; set; }
   }


controler:

liczbaDni = 1;
numerMiesiaca = 2;
numerRoku= 5;

 userDaty_Model userDaty = new userDaty_Model();
            userDaty.liczbaDniM = liczbaDni;
            userDaty.numerRokuM = numerMiesiaca;
            userDaty.numerMiesiacaM = numerRoku;
 return PartialView("_TableEve", userDaty);

and now I have for (razor) and how to throw in it the value assigned in the controller?
view
@for (int nr_rows = 0; nr_rows < @aandWhatWriteHere ; nr_rows++)





I tried:
DayValue : @Html.HiddenFor(item => item.userDaty_Model.liczbaDniM);   // does it works?

@for (int nr_rows = 0; nr_rows <  DayValue; nr_rows++)  // it does not work...
Posted
Updated 6-Feb-20 3:27am

1 solution

I googled "mvc viewbag in partial view" and this was the first result

Can't access ViewBag in a partial view in ASP.NET MVC3 - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900