Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("RegisterViewControl")

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.

like image 287
Shawn Mclean Avatar asked Jan 21 '11 20:01

Shawn Mclean


People also ask

Can partial view have controller?

It does not require to have a controller action method to call it. Partial view data is dependent of parent model. Caching is not possible as it is tightly bound with parent view (controller action method) and parent's model.

Can we use model in partial view?

Partial Views can use the Page Model for their data whereas Child Actions use independent data from the Controller. Editor/Display templates pass items from the model to the system but can be overridden by user partial views.

How do I render a partial view in view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

How do I return a partial view to another controller?

How to return a partial view from controller action method? To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.


1 Answers

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.

like image 143
Darin Dimitrov Avatar answered Oct 20 '22 16:10

Darin Dimitrov