Asp.Net Mvc Error Using RenderPartial
alt text http://a.imageshack.us/img709/5208/errorss.jpg
http://a.imageshack.u开发者_开发问答s/img709/5208/errorss.jpg
pls help, how to make ?
You are setting the viewData in an action within your controller, but calling render partial to display the partial view. The render partial never calls the action being used to generate the html, it is just passing the .ascx file into the browswer request. You either a) need to use html.renderaction
or b) pass the viewdata
in your renderpartial call.
A) <% Html.RenderAction("leftside", new { controller = "UserControls" }); %>
B)<% Html.RenderPartial("~/Views/Shared/UserControls/leftside.ascx", null, ViewData);%>
updated to C# (not sure on the part B, anyone check to make sure this is how not to send a model in c#)?
EDIT 2 - Part B will not work unless you set the ViewData in your parent controller/action calling the primary page. The only way to access the ViewData that you set in your leftside action is to call the RenderAction method in part A.
The error is caused by the fact the ViewData["mydata"]
is null, so calling ToString()
fails.
You can use Html.RenderAction or Html.RenderPartial in such scenarios
1) <% Html.RenderAction("leftside","UserControls");%> Or
2)<% Html.RenderPartial("leftside");%>
When Html.RenderPartial() is called with just the name of the partial view, ASP.NET MVC will pass to the partial view the same Model and ViewData dictionary objects used by the calling view template.
Please let me know if this works!
The problem is that your ViewData is in SiteMaster and from there u are rendering partial, so the partial does not see ViewData on the SiteMaster.
You need to pass the view data to the RenderPartial method so that Viewdata will be passed on to the partial view.
You can do so like this
<% Html.RenderPartial("partialViewName",ViewData) %>
精彩评论