Luke
Luke

Reputation: 409

Razor variable inside string html helper parameter

Here's may Html helper:

 @Html.ActionLink("CONTACT THE USER ABOUT THIS LISTING", "create", "contactform", new { recipientID = Model.User.Id, productid = Model.Product.ID }, null)

What I would like to do is:

 @Html.ActionLink("CONTACT " Model.UserName " ABOUT THIS LISTING", "create", "contactform", new { recipientID = Model.User.Id, productid = Model.Product.ID }, null)

Can it be done?

Upvotes: 0

Views: 1767

Answers (3)

mxmissile
mxmissile

Reputation: 11681

@Html.ActionLink($"CONTACT {Model.UserName} ABOUT THIS LISTING", "create", "contactform", new { recipientID = Model.User.Id, productid = Model.Product.ID }, null)

Upvotes: 0

mituw16
mituw16

Reputation: 5260

Yes, just use standard string concantenation.

@Html.ActionLink("CONTACT " + Model.UserName + " ABOUT THIS LISTING", "create", "contactform", new { recipientID = Model.User.Id, productid = Model.Product.ID }, null)

Upvotes: 0

David
David

Reputation: 219047

You can use the + operator to concatenate string values in C#:

"CONTACT " + Model.UserName + " ABOUT THIS LISTING"

Or something like string.Format() instead:

string.Format("CONTACT {0} ABOUT THIS LISTING", Model.UserName)

Upvotes: 1

Related Questions