0

I create mvc5 project.

I have iframe element in my view and @Model that I pass from controller:

@Model.MgPath = here is partial URL.

 <iframe src="http://localhost/mapfat/?WEBLAYOUT=" + @Model.MgPath width="100%" height="500px"></iframe>

in @Model.MgPath I have part of the URL that I need to attach to the soucre of iframe element.

My quetion is how to attach partial URL?It seems that attitude above is wrong.

0

1 Answer 1

1

You can use string.Format to build your url :

<iframe src="@(string.Format("http://localhost/mapfat/?WEBLAYOUT={0}",Model.MgPath))" width="100%" height="500px"></iframe>

Or the string interpolation (C# >= 6) :

<iframe src="@($"http://localhost/mapfat/?WEBLAYOUT={Model.MgPath}")" width="100%" height="500px"></iframe>

Or add a property to your model :

public class YourModel
{
       public string MapFatBaseUrl {get; private set;}
       public string MgPath {get;set;}
       public string MgPathURL => !string.IsNullOrWhiteSpace(MgPath) ? string.Format(@"{0}/?WEBLAYOUT={1}", MapFatBaseUrl, MgPath) : null;

       public YourModel(string mapFatBaseUrl){
              MapFatBaseUrl = mapFatBaseUrl;
       }
}

Than instanciate your model with new YourModel("http://localhost/mapfat") and in your view :

<iframe src="@Model.MgPathURL" width="100%" height="500px"></iframe>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for help.I try to use attitude above to generate relative path on img element: <img src="@(string.Format("~/assets/images/demo/Logo/{0}",Model.Logo))" width="45" height="45" alt="" /> But it seem to be wrong. Do you have any idea why?
can you post a screenshot of your solution explorer ? I would like to see you folder structure and where is the assets folder. If assets is at the root of your solution, you should remove the tild ('~') but I need to see how you have orgonized your solution to be sure about what's going wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.