RenderPartial control in same view folder
I have a file in my view folder Users
called UserViewControl.cshtml
.
My code in the actual view (Users.cshtml
) is:
@Html.RenderPartial("RegisterVie开发者_运维技巧wControl")
Error:The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
I do not want to type the path fully like this as the whole view folders might move in the future:
@Html.RenderPartial("~/Views/Users/RegisterViewControl.cshtml")
Code in 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)
}
This is a form that will be submitted by ajax, but I want all the validation from the viewmodel.
It should be like this:
@{Html.RenderPartial("RegisterViewControl");}
And that's because the RenderPartial
extension method doesn't return anything. It writes directly to the output. In aspx you use it like this:
<% Html.RenderPartial("RegisterViewControl"); %>
instead of:
<%= Html.RenderPartial("RegisterViewControl") %>
So the same rules apply for razor.
You could alternatively use
@Html.Partial("RegisterViewControl")
I had this issue as well and got this directly from Scott Guthrie's blog post:
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.
The link for this is: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
精彩评论