2011-01-21 80 views
9

我在我的視圖文件夾Users中有一個文件,名爲UserViewControl.cshtml在相同視圖文件夾中渲染部分控件

我在實際的視圖(Users.cshtml)的代碼是:

@Html.RenderPartial("RegisterViewControl") 

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

我不想類型的全是這樣的整體視圖文件夾的路徑可能會在未來的移動:

@Html.RenderPartial("~/Views/Users/RegisterViewControl.cshtml") 

守則RegisterViewControl.cshtml

@model SampleMVC.Web.ViewModels.RegisterModel 

@using (Html.BeginForm("Register", "Auth", FormMethod.Post, new { Id = "ERForm" })) 
{ 
    @Html.TextBoxFor(model => model.Name)   
    @Html.TextBoxFor(model => model.Email)    
    @Html.PasswordFor(model => model.Password)   
} 

這是一個將由ajax提交的表單,但我希望從viewmodel進行所有驗證。

+0

請留言添加了UserViewControl.cshtml – Clicktricity 2011-01-21 20:47:53

回答

23

它應該是這樣的:

@{Html.RenderPartial("RegisterViewControl");} 

這是因爲RenderPartial擴展方法不返回任何東西。它直接寫入輸出。在ASPX你使用這樣的:而不是

<% Html.RenderPartial("RegisterViewControl"); %> 

<%= Html.RenderPartial("RegisterViewControl") %> 

所以同樣的規則適用於剃刀。

7

你可以選擇使用

@Html.Partial("RegisterViewControl") 
0

我有這個問題,也是從斯科特Guthrie的博客中得到了直接的:

using @Html.RenderPartial() from a Razor view doesnt work.

Rather than call Html.RenderPartial() use just @Html.Partial("partialname")

That returns a string and will work.

Alternatively, if you really want to use the void return method you can use this syntax:

@{Html.RenderPartial("partialName")}

But @Html.Partial() is the cleanest.

的鏈接,這就是:http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx