简体   繁体   中英

How to pass anchor tag class Id to a function variable using mvc?

I need to pass anchor tag class name ID to a function var ID where var variable ID value should be passed in to the @Url.Action("pharmaCompanyDetails", "PharmaCompany", new { pharmaID = ID })

Variable ID value this passed into the pharmaID

Code:

<a class="detail" href="javascript:void(0)" title="Detail"><i class="glyphicon glyphicon-eye-open"></i></a> 

Button Click

'click .detail': function (e, value, row, index) {
                alert('You click like action, row: ' + JSON.stringify(row.id));
                var ID = JSON.stringify(row.id);
                window.location.href = '@Url.Action("pharmaCompanyDetails", "PharmaCompany", new { pharmaID = ID })';
            },

ID

在这段代码中工作

location.href = "@Url.Action("pharmaCompanyDetails", "PharmaCompany")?pharmaID=" + ID;
"@Url.Action("pharmaCompanyDetails", "PharmaCompany")" 

is rendered, server-side, by Razor and all your MVC firepower. When you land on the page it is already replaced by the url of your choice.

javascripts instead works on the client side, so it receive the id only when you click the button, long after your server side elaboration.

So,

'@Url.Action("pharmaCompanyDetails", "PharmaCompany", new { pharmaID = ID })'

ID cannot be used as a parameter inside this for this reason.

So you have to attach the ID to the end of the url, as you said:

location.href = "@Url.Action("pharmaCompanyDetails", "PharmaCompany")?pharmaID=" + ID;
location.href = "@Url.Action("actionname", "controller")?pharmaID=" + ID;

this c# code

@Url.Action("actionname", "controller")

will be converted to

controller/actionname

so full url will be

controller/actionname?pharmaID=ID;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM