Issue
I would like to call a Create View from the "Buchungen" controller.
To do this, I would like to add some data from another table (ArWoo) to the view so that it is already filled out in advance.
The two tables are not linked.
I give the appropriate ID when calling the "Buchungen" controller.
// GET: Buchungen/Create_AR
public IActionResult Create_AR(int? id)
{
var AR = _context.ArWoo
.Where(n => n.Id == id);
ViewData["Bestellnummer"] = AR.Bestellnummer;
return View("Create");
}
How can I now transfer a value from AR (e.g. "Bestellnummer") to the view?
I thought that would just go along with ViewData["Bestellnummer"] = AR.Bestellnummer;
But this doesn´t work.
If I set a breakpoint at return View ("Create"), I see that the variable AR is correctly assigned.enter image description here
Solution
Fix the action by adding FirstOrdefault to a query
public IActionResult Create_AR(int? id)
{
var AR = _context.ArWoo
.Where(n => n.Id == id).FirstOrDefault();
ViewData["Bestellnummer"] = AR.Bestellnummer;
// or return View("Create",AR.Bestellnummer);
return View("Create");
}
Answered By – Serge
Answer Checked By – David Goodson (BugsFixing Volunteer)