开发者

Error in RenderPartial in loop asp.net mvc

开发者 https://www.devze.com 2023-03-01 15:50 出处:网络
I am trying to render multiple Views using a loop like this 开发者_JS百科 @model IEnumerable<RamtaJogi.Web.Razor.Controllers.IMenuRenderer>

I am trying to render multiple Views using a loop like this 开发者_JS百科

@model IEnumerable<RamtaJogi.Web.Razor.Controllers.IMenuRenderer>

@foreach (var item in @Model)
{
    {@Html.RenderPartial(@item.ViewName, @item.ViewData);}
     <br />
}     

here is my IRenderer

public interface IMenuRenderer     
{
    string ViewName { get; }
    object ViewData { get; }
}

But it throws an error CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

I pass a collection of objects of type IMenuRenderer to my view.

Any idea what is wrong with my codes. Can someone help here.

Regards

Parminder


You have too many @ characters.

You should only use @s to enter the foreach.
Since RenderPartial returns void, you cannot print its result with an @.
Parameters also never get @s.

Change your code to

@model IEnumerable<RamtaJogi.Web.Razor.Controllers.IMenuRenderer>

@foreach (var item in Model)
{
    @Html.Partial(item.ViewName, item.ViewData)
     <br />
}     


You can create a custom helper.I think this will help you. ASP.NET MVC: Custom Html Helpers in Razor

0

精彩评论

暂无评论...
验证码 换一张
取 消