0

I'm trying to pass data from my controller to a partial view using ViewData but it's not working correctly.

code in controller

public PartialViewResult PartFace()
    {

        string message = "You are not connected to Facebook";
        string name = "";
        string appid = ConfigurationManager.AppSettings["AppID"];
        FacebookConnect fbConnect = new FacebookConnect();
        if (fbConnect.IsConnected)
        {
            message = "You are connected to Facebook";
            string token = fbConnect.AccessToken;
            token = HttpUtility.UrlDecode(token);
            Facebook.FacebookAPI api = new Facebook.FacebookAPI(token);
            JSONObject me = api.Get("/" + fbConnect.UserID);
        }
        ViewData["Message"] = message;
        ViewData["AppID"] = ConfigurationManager.AppSettings["AppID"];
        ViewData["Name"] = name;
        return PartialView();
    }

code in partial view

<h2><%: ViewData["AppID"]%></h2>
<% if (!string.IsNullOrEmpty((string)ViewData["Name"])) { %>
<h2>Hello, <%: (string)ViewData["Name"]%> </h2>
<% } %>
<p>
    <fb:profile-pic uid="loggedinuser" facebook-logo="true" linked="false"></fb:profile-pic>
    <fb:login-button autologoutlink='true' onlogin='window.location.reload()' perms='read_stream,publish_stream,read_friendlists,user_activities'></fb:login-button>
</p>
 <div id="fb-root"></div>
<script type='text/javascript'>
    window.fbAsyncInit = function () {
        FB.init({ appId: '<%= ViewData["AppID"] %>', status: true, cookie: true,
            xfbml: true
        });
    };
    (function () {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    } ());
</script>

code to call this partial in master page

<% Html.RenderPartial("~/Views/Home/PartFace.ascx"); %>
Dismissile
  • 32,564
  • 38
  • 174
  • 263

3 Answers3

1

Create a ViewModel:

    public class ViewModel
    {
        public string Message { get; set; }
        public int AppId { get; set; }
        // ...
    }

Pass it to you view like this

return View(new ViewModel() { Message = "Works" });

then you can access this in you view by setting

@model ViewModel;

@Model.Message 

if you want to pass informations to a partial view you can do this (in your view)

Html.RenderPartial("~/Views/Home/PartFace.ascx", Model) 

if you want to pass your model or

Html.RenderPartial("~/Views/Home/PartFace.ascx", Model.AppId)

if you want the appid getting passed to your partial.

hope this helps

dknaack
  • 60,192
  • 27
  • 155
  • 202
1

You render the partial view directly. In order to get the data from the controller you should call RenderAction like this:

<% Html.RenderAction("PartFace"); %>

Note that if the call is from a view rendered by a different controller than the one containing the PartFace action you should also specify the corresponding controller:

<% Html.RenderAction("PartFace", "[controller]"); %>
shizik
  • 910
  • 6
  • 16
0

I had similar problem, this solution did work for me.

  1. Created ViewDataDictionary collection
  2. assigned the collection to viewData property of a partial before return.

    public PartialViewResult PartFace() {
    
    string message = "You are not connected to Facebook";
    string name = "";
    string appid = ConfigurationManager.AppSettings["AppID"];
    FacebookConnect fbConnect = new FacebookConnect();
    if (fbConnect.IsConnected)
    {
        message = "You are connected to Facebook";
        string token = fbConnect.AccessToken;
        token = HttpUtility.UrlDecode(token);
        Facebook.FacebookAPI api = new Facebook.FacebookAPI(token);
        JSONObject me = api.Get("/" + fbConnect.UserID);
    }
    
    ViewDataDictionary viewData = new ViewDataDictionary();
    viewData.Add("Message", message);
    viewData.Add("AppID",  ConfigurationManager.AppSettings["AppID"]);
    viewData.Add("Name",  name);
    
    var partialView = PartialView();
    partialView.ViewData = viewData;
    
    return partialView;
    }