MVC, C # Passing parent.id to a child index or Create an action?

The closest I found to my question was, but I do not understand how it will be used.

<% object parentModel = ViewContext.ParentActionViewContext.ViewData.Model; %>

I have a gateway table and a sensor table . Gateways can have several sensors. The tables have a relationship from 1 to many (gateway to the sensor), and the sensor table has a gateway field through which the sensor is connected to the gateway.

I use an entity structure I have a gateway repository class I have a gateway controller controller For processing data annotations, I have a class of partial gateway assistants I have an index that lists gateways and provides classic crud pages

In the list of gateways, I added an action link to list the sensors associated with this gateway.

Everything is still (hopefully).

I assume that now I have to create the same setting for my sensor table; repository, controller, class of friends, etc.

Q1: What is the MVC approach for listing all sensors (child records) for a given gateway (parent record)?

In each line of the gateway position, I created a link that looks like this:

localhost/sensor/list/5

This would mean that only sensors for the current gateway should be displayed in the list.

I do not think this is the right way. I think I need to skip the gateway model in the background so that at any time I call a link like this:

localhost/sensor/

he would just understand the context of the current gateway.

Q2: , . , , ?

SensorController:

SensorRepository repository = new SensorRepository();
GatewayRepository Grepository = new GatewayRepository();

ActionResult :

public ActionResult List(int id)
{
    Gateway gateway = Grepository.GetGateway(id);

    var sensors = repository.FindGatewaySensors(id);
    return View(sensors);
}

ActionResult ( , gatewayid , ):

// GET: /Sensor/Create
public ActionResult Create(int id)
{
    Sensor sensor = new Sensor();

    sensor.gatewayid = id;
    return View(sensor);

} 

Create ActionResult:

[HttpPost]
public ActionResult Create(FormCollection collection)
{

    Sensor sensor = new Sensor();

    if (TryUpdateModel(sensor))
    {
        repository.Add(sensor);
        repository.Save();
        return RedirectToAction("List", new { id = sensor.gatewayid });
    }
    return View(sensor);
}

. , . , .

+3
1

, ( ), ( ).

Q1 , , , "" URL- MVC, , URL- , . - , , :

//5/sensorlist

. , "" , , cookie , - URL-, , : " !" - ( ID, , - ).

Q2, , , , , . , , .

, , !:)

0

Source: https://habr.com/ru/post/1779099/