Question

I am trying to get a model value in MVC codescope-

@model FunRanger.Models.CardModel
@{
    ViewBag.Title = "CardDetails";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var link = string.Format(
    "'http://www.facebook.com/sharer.php?u='+'http://funranger.com/Home/CardDetails/?cardID'+'@Model.cardID",
    Url.Encode("http://funranger.com/Home/CardDetails/?cardID=@Model.cardID/"),
    Url.Encode("This is my site")
);

}

Here you can see that I am trying to get model value in var link and sharing it on facebook. Here in this codescope I want to get @Model.cardID's value.

How do I get this model value?

For example URL would be like- http://funranger.com/Home/CardDetails/?cardID=178

Edit-

 string encodedUrl = String.Format("http://funranger.com/Home/CardDetails/?cardID={0}", Model.cardID);
    string link = string.Format("http://www.facebook.com/sharer.php?u={0}", Url.Encode(encodedUrl));

It is shared on my website with encodeUrl path.

Facebook sharing shows this-

enter image description here

It seems to be sharing an Image.

I am sharing it as below-

 <a href="@link">share in facebook</a>
Was it helpful?

Solution

For example URL would be like- http://funranger.com/Home/CardDetails/?cardID=178

you can get it like this -

@{
    string encodedUrl = String.Format("http://funranger.com/Home/CardDetails/?cardID={0}/",Model.cardId);
    string link = string.Format("http://www.facebook.com/sharer.php?u={0}", Url.Encode(encodedUrl));
}

Output -

enter image description here

Between I am confused with your usage of String.Format(), you have not mentioned placeholders any where in your String.Format() implmentation.

Also I am not sure about Url.Encode("This is my site")

OTHER TIPS

You Are appending the Model.cardId in wrong way. I am not sure what is the use of the other two arguments in the string.Format in y our case. Any way you need to append the value in these ways.

1. var link = "http://funranger.com/Home/CardDetails/?cardID=" + Model.cardID;
2. var link = string.Format("http://funranger.com/Home/CardDetails/?cardID={0}",Model.cardID); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top