Set asp-route-id dynamically

Dondon510 221 Reputation points
2022-05-06T05:03:11.223+00:00

Hi

I have 2 asp-route-id values as below:

<a class="btn btn-block btn-primary" asp-controller="Sales" asp-action="Data" asp-route-sales_pid="0" asp-route-users_pid="users_pid">New</a>

the above is used for a new sales transaction, when it clicked, I expect to set asp-route-users_pid with current users_pid.

how to set its value?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2022-05-19T03:04:25.737+00:00

    Hi @Dondon510 ,

    < a id="xyz" class="c-header-nav-link text-black-50" asp-controller="Inbox" asp-action="Index" asp-route-tenants_pid="[@](/users/na/?userId=0f55de7e-bffd-0003-0000-000000000000).tenants_pid" asp-route-users_pid="users_pid" asp-route-sales_type="sales_type"></a>  
    

    as you see, there are asp-route-tenants_pid was automatically filled by @默 .tenants_pid and I use variable to asp-route-users_pid And asp-route-sales_type
    question:
    how do I pass the value to users_pid And sales_type?

    To set the route parameter, you could refer the following methods:

    1. set the route parameter via the page model
      @model WebApplication6.Models.TenantsSale  
      @{  
          ViewData["Title"] = "TenantSalesIndex";  
          Layout = "~/Views/Shared/_Layout.cshtml";  
      }   
      <h2>set the route parameter via the page model</h2><br />  
      <a id="xyz" class="c-header-nav-link text-black-50" asp-controller="Home" asp-action="Index"   
          asp-route-tenants_pid="@Model.tenants_pid"   
          asp-route-users_pid="@Model.users_pid"   
          asp-route-sales_type="@Model.sales_type">Link A</a><br />  
      
    2. set the route parameter via the @{} code block: in the code block, define the variable and set the value, then use them on the a tag asp-route-{value} attribute.
      <h2> set the route parameter via the code block</h2><br />  
      @{  
          var users_pid = 1002;  
          var sales_type = "TypeB";  
      }  
      <a id="xyz" class="c-header-nav-link text-black-50" asp-controller="Home" asp-action="Index"   
          asp-route-tenants_pid="@Model.tenants_pid"   
          asp-route-users_pid="@users_pid"   
          asp-route-sales_type="@sales_type">Link B</a><br />  
      
    3. set the route parameter via the JS: use JS to get the entered value and append the parameter to the request url.
      <h2> set the route parameter via the JS</h2><br />  
      <div class="row">  
          <div class="col-md-4">    
                  <div class="form-group">  
                      <label class="control-label">New Users_Pid</label>   
                      <input id="txtuser_pid" class="form-control" value="" />   
                  </div>  
                    <div class="form-group">  
                      <label class="control-label">New Sales_type</label>   
                      <input id="txtsales_type" class="form-control" value="" />   
                  </div>   
                  <div>  
                      <div class="form-group">   
                           <a id="xyz_js" class="c-header-nav-link text-black-50"  
                              asp-controller="Home" asp-action="Index"   
                              asp-route-tenants_pid="@Model.tenants_pid">Link C</a>  
                      </div>  
                  </div>  
          </div>  
      </div>   
      
      The JS code as below: 203562-pagecode.txt
      203492-image.png

    And the TenantsSale model and the controller like this:

    public class TenantsSale  
    {  
        public int tenants_pid { get; set; }  
        public int users_pid { get; set; }  
        public string sales_type { get; set; }  
    }  
    

    controller:

    public IActionResult TenantSalesIndex()  
    {  
        var item = new TenantsSale()  
        {  
            tenants_pid = 1001,  
            users_pid = 101,  
            sales_type = "TypeA"  
        };  
        return View(item);  
    }  
    
    public IActionResult Index(int tenants_pid, int users_pid, string sales_type)  
    {  
        return RedirectToAction(nameof(TenantSalesIndex));  
    }  
    

    Finally, the result as below(pay attention to the URL at the left bottom of the browser):

    After rendering the a tag's href attribute like this: href="/?tenants_pid=1001&users_pid=101&sales_type=TypeA", and the URL like this: https://localhost:7070/?tenants_pid=1001&users_pid=101&sales_type=TypeA.

    203532-2.gif

    More detail information about set the parameter to the Anchor tag, see Anchor Tag Helper in ASP.NET Core


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Dondon510 221 Reputation points
    2022-05-15T06:50:41.63+00:00

    Hi

    I have this:

    <a id="xyz" class="c-header-nav-link text-black-50" asp-controller="Inbox" asp-action="Index" asp-route-tenants_pid="@Model.tenants_pid" asp-route-users_pid="users_pid" asp-route-sales_type="sales_type"></a>  
    

    as you see, there are asp-route-tenants_pid was automatically filled by @默 .tenants_pid and I use variable to asp-route-users_pid And asp-route-sales_type

    question:
    how do I pass the value to users_pid And sales_type?

    0 comments No comments

  2. Bruce (SqlWork.com) 55,041 Reputation points
    2022-05-15T16:47:10.97+00:00

    In razor syntax, you prefix the variable name with an @

    asp-route-users_pid="@users_pid"
    

    But users_pid must be a variable defined in a razor code block. Probably you need to pass them a model properties or as ViewData

    0 comments No comments

  3. Dondon510 221 Reputation points
    2022-05-19T03:14:44.163+00:00

    Excellent!

    thanks @Zhi Lv - MSFT

    0 comments No comments