RenderPartial控件位于同一视图文件夹中

Sha*_*ean 9 asp.net-mvc asp.net-mvc-3

我的视图文件夹中有一个Users名为的文件UserViewControl.cshtml.

我在实际视图(Users.cshtml)中的代码是:

@Html.RenderPartial("RegisterViewControl")
Run Code Online (Sandbox Code Playgroud)

错误: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")
Run Code Online (Sandbox Code Playgroud)

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)          
}
Run Code Online (Sandbox Code Playgroud)

这是一个由ajax提交的表单,但我希望从viewmodel进行所有验证.

Dar*_*rov 23

它应该是这样的:

@{Html.RenderPartial("RegisterViewControl");}
Run Code Online (Sandbox Code Playgroud)

那是因为RenderPartial扩展方法没有返回任何内容.它直接写入输出.在aspx中,你可以像这样使用它:

<% Html.RenderPartial("RegisterViewControl"); %>
Run Code Online (Sandbox Code Playgroud)

代替:

<%= Html.RenderPartial("RegisterViewControl") %>
Run Code Online (Sandbox Code Playgroud)

所以同样的规则适用于剃须刀.


Dea*_*ean 7

你也可以使用

@Html.Partial("RegisterViewControl")
Run Code Online (Sandbox Code Playgroud)