2

I have a mvc view which needs to show the twitter feeds. So i have the code generated from twitter in my view. This works fine.

To extend this i want to make this a little dynamic. ie i want to hardcoded data-widget-id="3831079557xxxx" to a value from a database table.

Below is the javascript generated by twitter to embed the tweets

<div class="row">
   <div class="col-md-12">
      <a class="twitter-timeline" href="https://twitter.com/xxxxx" 
          data-widget-id="3831079557xxxx">Tweets by xxxx</a>
              <script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + "://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); } }(document, "script", "twitter-wjs");</script>



                </div>
            </div>

View Model:

public class homeViewModel
{
    public List<news> newsA { get; set; }

    public List<Rss> feeds { get; set; }

    public List<HomepageSetting> hps { get; set; }

}

Model: This is the class from which i can get the twitter id

public partial class HomepageSetting
    {
        public int Id { get; set; }
        //other properties
        public string Twitter { get; set; }

    }

Controller:

public ActionResult Index()
    {
        homeViewModel hvm = new homeViewModel();
        var data = (from p in db.HomepageSetting where p.ProfileID == Cuser.ProfileId select p);
                hvm.hps= data.ToList();
     return View(hvm);
     }
1
  • 1
    Unclear way to ask. I think if you want to get answer need more description about your problem. Commented Dec 24, 2014 at 9:20

2 Answers 2

3

In the controller Index method you can use the ViewData Dictionary to pass the id to the view:

ViewData["id"] = data.id //for example.

In the view, you can use the ViewData["id"] entry to assign it to data-widget-id attribute.

<a class="twitter-timeline" href="https://twitter.com/xxxxx" 
          data-widget-id="<%: ViewData["id"] %>">Tweets by xxxx</a>

Good Luck

Sign up to request clarification or add additional context in comments.

2 Comments

You probably want to use data-widget-id=@ViewData["id"]
It depend actually on the view engine used. The solution i wrote is for the ASPX engine. Your solution is for Razor engine. @Prady
1

You can use Razor Framework and have in the javascript

@foreach (var myItem in Request.ServerVariables)
{
    <a class="twitter-timeline" href="https://twitter.com/xxxxx" 
          data-widget-id="3831079557@myItem ">Tweets by xxxx</a>
}

I cannot give you more explaining code, because this is something I was working on MVC4. I am no longer working with the Microsoft Framework sothe code will not be fully functional.

Check here too for more information in Razor Framework.

1 Comment

For example, for Request.ServerVariables you can load your model with a name in the top of your view, and then call it in the foreach loop.

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.