2011-01-21 21 views
9

Mam plik w moim folderze widoku Users o nazwie UserViewControl.cshtml.Kontrolka RenderPartial w tym samym folderze widoku

Mój kod w rzeczywistym widoku (Users.cshtml) wynosi:

@Html.RenderPartial("RegisterViewControl") 

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

Nie chcę, aby wpisać ścieżkę w pełni tak jak cały widok folderów może poruszać się w przyszłości :

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

kod w 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)   
} 

To jest formularz, który zostanie przesłany przez ajax, ale chcę wszystkie walidacje z viewmodel.

+0

proszę pisać kod w UserViewControl.cshtml – Clicktricity

+0

@ Clicktricity added. –

Odpowiedz

23

To powinno być tak:

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

A to dlatego, że metoda RenderPartial rozszerzenie nic nie wróci. Zapisuje bezpośrednio na wyjście. W aspx go używać tak:

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

zamiast:

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

Więc te same zasady stosuje się do maszynki.

7

Można alternatywnie użyć

@Html.Partial("RegisterViewControl") 
0

miałem tego problemu, jak również i dostaliśmy to bezpośrednio od Scotta Guthrie blogu:

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.

Link do tego jest: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

Powiązane problemy