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"})
精彩评论