I have these classes
public Customer
{
public int ID { get; set; }
// [...] some other properties
}
public CustomerDetails
{
public Customer Customer { get; set; }
// [...] some other properties
}
i have this controller action
[HttpGet]
[Route("{customer.id}")]
public async Task<IActionResult> Details([FromQuery]CustomerDetails model)
{
// [...]
}
i also have tried [Route("{model.customer.id}")]
and [Route("{id}")]
and added [FromQuery]
to CustomerDetails.Customer
but model.customer.id
is always 0
.
the action should have the route /customers/{model.customer.id}. How can I achieve this?
for url
/customers/details/5
change your action routing to this:
[Route("~/customers/details/{customerId}")]
public async Task<IActionResult> Details(int customerId)
{
// [...]
}
if your want url
/customers/5
you need this route attribute
[Route("~/customers/{customerId}")]
public async Task<IActionResult> Details(int customerId)
{
// [...]
}
if you add CustomerId to your model
public CustomerDetails
{
public int CustomerId { get; set; }
public Customer Customer { get; set; }
// [...] some other properties
}
it will we be working too
[Route("~/customers/details/{customerId}")]
public async Task<IActionResult> Details(CustomerDetails model)
{
// [...]
}
i also have tried [Route("{model.customer.id}")] and [Route("{id}")] and added [FromQuery] to CustomerDetails.Customer but model.customer.id is always 0.
the action should have the route /customers/{model.customer.id}. How can I achieve this?
it is working with /customers/details?customer.id=5 but i want it to be /customers/5
According to your code and description, I suppose perhaps you want to get the parameter from route, instead of the query.
Try to modify your code as below:
Use [FromRoute]
attribute.
From your description, it seems that you just want to pass the customer id to the action method, so there is no need to use the CustomerDetails as parameter.
[HttpGet] [Route("Test/Details/{customerid}")] public IActionResult Details([FromRoute]int customerid) { // [...] ViewBag.CustomerId = customerid; return View(); }
Code in the View Page:
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
@ViewBag.CustomerId
The result like this:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.