2011-01-21 25 views
9

Tôi có một tệp trong thư mục xem của mình Users được gọi là UserViewControl.cshtml.Điều khiển RenderPartial trong cùng một thư mục xem

Mã của tôi trong giao diện thực tế (Users.cshtml) là:

@Html.RenderPartial("RegisterViewControl") 

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

Tôi không muốn phải gõ đường dẫn đầy như thế này như các thư mục toàn bộ xem có thể di chuyển trong tương lai :

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

Mã trong 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)   
} 

Đây là biểu mẫu sẽ được gửi bởi ajax, nhưng tôi muốn tất cả xác thực từ chế độ xem.

+0

xin vui lòng gửi mã trong UserViewControl.cshtml – Clicktricity

+0

@Clicktricity được thêm vào. –

Trả lời

23

Nó phải là như thế này:

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

Và đó là bởi vì phương pháp RenderPartial gia hạn không trả lại bất cứ điều gì. Nó viết trực tiếp vào đầu ra. Trong aspx bạn sử dụng nó như thế này:

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

thay vì:

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

Vì vậy, các quy tắc tương tự áp dụng cho dao cạo.

7

Hoặc bạn có thể sử dụng

@Html.Partial("RegisterViewControl") 
0

Tôi có vấn đề này như là tốt và đã trực tiếp này từ bài blog của Scott 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.

Liên kết cho điều này là: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

Các vấn đề liên quan