MVC 4 how pass data correctly from controller to view
You should create a ViewModel
with all of your data needed and then pass that down to the view.
public class ViewModel
{
public List<int> Melt1 { get; set; }
public void LoadMeltProperties()
{
if (Melt1 == null)
{
Melt1 = new List<int>();
}
Melt1 = (from item in db.tbl_dppITHr
where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
select item).Sum(x => x.Furnace1Total).ToList();
}
public ViewModel Load()
{
LoadMeltProperties();
return this;
}
}
public ActionResult YourControllerAction()
{
var vm = new ViewModel().Load();
return View("ViewName", vm);
}
Then in your View you can use a strongly typed
model rather than dynamic
@model ViewModel
You can then iterate over your ViewModel properties via:
foreach(var melt in Model.Melt1) {
// do what you require
}
EDIT NEW Pass data from the controller to the view using HttpPost
It would be easier for you to create a View Model and put the data in it and >return to View with the filled ViewModel Object this could look sth like this
The View Model
public class PartialTableEcpModel
{
public List<string> Days {get; set;}
//And so on
}
The Controller
public ActionResult PartialTabelaEcp(string json)
{
PartialTableEcpModel tableModel = new PartialTableEcpModel();
tableModel.Days = new List<string>() {"Day1", "Day2", Day3};
var nr_days= 31;
ViewBag.days= nr_days;
return PartialView("_TableEve", tableModel);
}
The Razor View
@model PartialTableEcpModel
@foreach(var day in Model.Days)
{
<p>@day</p>//or whatever
}
so it is again for a Get request
For Edit
you have to set the
@model
thing at the top of the RazorView.cshtml to your model, then theModel
property in your is the Model you push in the Controller
Passing data from a controller to a view (MVC)
in your foreach you need to use the variable that you have defined. In this situation I don't think you need the helper. Try changing your code to
@foreach(var invalid in ViewBag.InvalidParts){
<text>
<h2>@invalid</h2>
</text>
}
MVC 4 - Pass Data from View = Controller
Posting as an answer as this is too long to comment. I have tried to recreate and it is all working here so I thought I would post what I have so you can compare with what you are doing:
I am assuming that Kauf.Value
is a string here...
Controller
[HttpGet]
public ActionResult Create()
{
// Setup model before passing in
var model = new Bond();
return View(model);
}
[HttpPost]
public ActionResult Create(Bond position)
{
string theValue = position.Kauf.Value;
// At this point "theValue" contains a valid item
return View(position);
}
View
@model MvcExperiments.Models.Bond
@using(@Html.BeginForm())
{
<div class="editor-field">
@Html.EditorFor(model => model.Kauf.Value)
@Html.ValidationMessageFor(model => model.Kauf.Value)
</div>
<p>
<input type="submit" value="Save" />
</p>
}
How to pass Session data from controller to View (MVC)
If it is for saving with entity, there is no need to pass it to the view and send it back to server. You can use it directly in your HttpPost action method.
[HttpPost]
public ActionResult Create(Review review)
{
if (ModelState.IsValid)
{
if(Session["LoggedUserID"]==null)
{
return Content("Session LoggedUserID is empty");
}
review.UserID = Convert.ToInt32(Session["LoggedUserID"]);
db.Reviews.Add(review);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(review);
}
I also added an If condition to check whether Session["LoggedInUserID"]
is null before reading it to an int variable( UserID
property). Ideally you may move this out of the action method to check whether user is logged in or not (May be an action filter like this)
ASP MVC 4 - binding and passing data from view to controller in repeating controls
Unfortunately could not post it earlier due to reputation regulations.
Finally resolved this issue due to good explanation at:
HTML.ActionLink method
When you try to pass an argument from Url.Action or Html.ActionLink - you have to specify explicitly the final "null" argument responsible for html arguments.
In my case the following code works correctly:
slightly changed controller action (now receives just name instead of commitee object itself)
public ActionResult CommiteePage(string commiteeName)
{
return View("CommiteePage", SecureFolder.Commitees.First(o=>o.Name == commiteeName));
}
and changed syntax for html calling this action:
@foreach (var commitee in Model.Commitees)
{
<a href="@Url.Action("CommiteePage", "SecureFolder", new { commiteeName=commitee.Name }, null)">
<div class="commiteeButtonImageContainer">@commitee.Name</div>
<img src="~/Images/CommiteeButtonImage.png"/>
</a>
}
Now view correctly passes the name of selected commitee to controller so that I can redirect to corresponding commitee view.
Thank you all for helping to resolve this issue!
Related Topics
How to Format Timespan in Xaml
"The Linq Expression Node Type 'Invoke' Is Not Supported in Linq to Entities" - Stumped!
..The Underlying Connection Was Closed: an Unexpected Error Occurred on a Receive
Will Using Linq to SQL Help Prevent SQL Injection
Keep Casing When Serializing Dictionaries
How to Read Image Pixels' Values as Rgb into 2D Array
How to Call the Parent Version of an Overridden Method? (C# .Net)
File Write Permission Issue Under "Program Files" Folder
What Do the "+N" Values Mean at the End of a Method Name in a Stack Trace
How to Print PDF on Default Network Printer Using Ghostscript (Gswin32C.Exe) Shell Command
Relative Path to Absolute Path in C#
Exporting Datagridview to CSV File
What's the Difference Between Double Quotes and Single Quote in C#
Is There an Exponent Operator in C#
Entity Framework Ef.Functions.Like VS String.Contains
Problems Overwriting (Re-Saving) Image When It Was Set as Image Source