I'm trying to learn ASP.Net Core, and I'm having trouble figuring out how to move data around in the MVC 'style'(?). The ASP.NET Core tutorial on Microsoft's website that I'm following doesn't deal with any relations between models. (seems to be an stupid oversight on their part?) Maybe my Google-Fu is off, so if this is obvious, or anyone has some reading material I can look at for ASPNETCore, I'd appreciate it.
I've got two Models. One called Device and another called DeviceType. In my Device Model, I've got a reference to a DeviceType attribute.
public class Device{
[Key]
public int ID {get; set;}
[Required]
[Display(Name = "Device Name")]
public String deviceName {get; set;}
[Required]
[Display(Name="Description")]
public String deviceDescription {get; set;}
[Required]
[Display(Name="Type")]
public DeviceType deviceType{get; set;}
}
public class DeviceType{
[Key]
public int ID {get; set;}
[Required]
[Display(Name="Type")]
public String name {get;set;}
[Required]
[Display(Name="Description")]
public String description {get;set;}
}
I want to figure out how to - for each action (CRUD) - reference and get the deviceType for the particular Device object. I also don't understand how to reference the related Device Type when I work on my Views.
GET Create * How do I get a SelectList of my DeviceTypes so that a user can create a Device and choose what type of Device it is? * I've done some research on the View portion, and it looks like I need to use the shorthand Razor "@model.SelectList()" syntax, but the parameters are confusing for me. I don't understand exactly what it's asking for. Here's what the controller looks like right now for that action.
public IActionResult Create()
{
return View();
}
Index * In the loop showing all devices, how would I also include the name of the deviceType in the resultant table of devices? * In the view, I would assume I could use @Html.LabelFor to show what the data's name is, but how would I get the actual values?
public async Task<IActionResult> Index()
{
return View(await _context.Device.ToListAsync());
}
Update/Edit * How would I retrieve the Device Objects' DeviceType, and then allow the user to edit it?
// GET: Devices/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var device = await _context.Device.SingleOrDefaultAsync(m => m.ID == id);
if (device == null)
{
return NotFound();
}
return View(device);
}
Delete * I haven't looked at this yet, but I'm putting it here in case theres any caveats when programming other actions...
// GET: Devices/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var device = await _context.Device
.SingleOrDefaultAsync(m => m.ID == id);
if (device == null)
{
return NotFound();
}
return View(device);
}
Again, if there's some kind of resource I can look at other than Microsoft's "build an asp.net core application with Mac and Linux with Visual Studio Code", that would explain how to reference relations in models inside of views/controllers, that would be great.
Thank you