开发者

RenderPartial not working

开发者 https://www.devze.com 2023-03-16 14:53 出处:网络
I have a simple partial view which returns (renders) a list of synonyms of a given word. Then I\'d like to use thi开发者_如何学Gos partial view inside another view and I use @Html.RenderPartial(\"Syno

I have a simple partial view which returns (renders) a list of synonyms of a given word. Then I'd like to use thi开发者_如何学Gos partial view inside another view and I use @Html.RenderPartial("SynonymFinder", new { word = "Something" }) inside my view. But I get this error:

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

This is the simplest scenario. I even removed the parameters and used @Html.RenderPartial("SynonymFinder"), but still the same problem. What's wrong?


In MVC 3 you should use:

@Html.Partial("SynonymFinder", new ViewDataDictionary { { word = "Something" } })

Note that the 2nd parameter is of type ViewDataDictionary. If you don't pass it explicitly like that, the helper will use the overload that takes an object as the 2nd parameter and uses it as a Model instead of as route values.


You need to create a model with the field word

public class SynonymFinderModel
{
    public string Word {get; set;}
}

Then, in your view, you have

@Html.Partial("SynonymFinder", new SynonymFinderModel { Word = "something"})
0

精彩评论

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

关注公众号