I am assuming there is a way to set a default layout to use if one is not selected, but I cant seem to find any documentation on it.
From the controller, if I call return View() it returns the page without any of the html from the layout. If i return the view as follows, the layout shows up fine.
return View("Index", "~/Views/Shared/_PublicLayout.cshtml");
So I'm wondering if there is a way to set the default variable for the layout so i don't have to specify it every time for every view.
In the views folder locate or create file _ViewStart.cshtml and inside you can define:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
where _Layout.cshtml is your default layout.
Related
I am curious to know if it is possible to add a fragment to a view model:
return new ViewModel([]);
Returns my view to https://example.com/view
However is it possible to set this dynamically to add a fragment?
return new ViewModel("#fragment")
To return to https://example.com/view#fragment
The use case, have a js login/register view that is either https://example.com/view#login or https://example.com/view#register and want to return to the correct view...
EDIT
Obviously, this can be done using a re-direct, however, in the case of returning form errors, ViewModel needs to be used...
Not sure if this is possible to apply param to router, but you can alwas return redirectToRegister = true, and then write and execute JavaScript in view which will change view if True.
A fragment is a same document reference i.e. if the URL specification is used correctly #register and #login will refer to different elements in the same document.
The view model therefore should be returning the same html in both cases, and the fragment will be handled by the browser. To distinguish register and login pages instead use different paths in the URL.
I'm converting an MVC aspx content place holder -> master page to an MVC razor section -> layout.
In the past when my aspx view came to something like this:
<asp:Content ID="HelpContent" ContentPlaceHolderID="HelpLink" runat="server">
Help
</asp:Content>
And the master page didn't have a corresponding HelpContent place holder (perhaps because a user was not authenticated) everything rendered fine (with no HelpContent section).
Now when I have a razor section defined that does not have a corresponding #RenderSection in the layout, I get this error:
The following sections have been defined but have not been rendered
for the layout page "~/Views/Shared/New.cshtml": "HelpLink".
Do I need to redesign this?
Is there a way I can have the view's HelpLink section render optionally if the layout gives it the green light?
EDIT:
I think there's some confusion, so let me re-summarize:
The layout logic looks like this:
if (User.IsLoggedIn) {
#RenderSection( "HelpLinks", false);
}
But then the user isn't logged in, it doesn't render, and then .NET throws an exception because the layout doesn't know what to do with the section.
You can indicate that the section is optional by passing false as the second argument:
#RenderSection("HelpLink", false);
Edit: In the case of control flow logic for rendering, you can use .NET in the razor view (like this c# example):
#if(IsSectionDefined("HelpLink"))
{
#RenderSection("HelpLink", false);
}
Or, if you want to base rendering on whether the user is logged in, you could replace the if logic in the above sample with your security check.
Edit 2:
Make sure you have defined the section:
#section HelpLink {
//This needs to be defined in any view that uses the layout with the #RenderSection. It can be empty.
}
Alternatively, you can add the check to see if the section exists and only define the #section in the required view:
if (IsSectionDefined("HelpLink") && User.IsLoggedIn) {
#RenderSection( "HelpLinks", false);
}
If a section is declared in a razor view it has to be rendered in the layout.
I found this in Freeman's Pro ASP.NET MVC 5 book.
Seems like a bad design to me.
I am trying to create an asp.net MVC website and noticed that every view I create always has the header at the top and footer at the bottom due to the shared layout template. Is there a way to create a view that wont follow the template?
You can use multiple layouts.
Just create a second layout without the css reference.
You can specify layouts at the top of your view like this :
#{
Layout = "~/Views/Shared/_PublicLayout.cshtml";
}
You can do this a ton of ways. Here are the two most common ways.
In your controller you can..
return this.PartialView(model);
or in your View you can (I don't recommend this..)
#{
Layout = null;
}
I have multiple views/models that are being looped into the main view. I load them via #Html.Partial()...These views/models are basically form elements with certain properties...Unfortunately I soon found out that only the first of each type of view/model is validated. I tried moving the fields around and only the first of each kind would validate.
My partial views look something like this:
#Html.DropDownListFor(model=>model.dropdownVal,Model.SelectItems,new { id=Model.FieldID, Name = Model.FieldID })
I looked at the HTML rendered, and it seems that the validation tags like "data-val" are not applied...
Any ideas would be greatly appreciated!
Add the following at the top of your partials to trick ASP.NET MVC into thinking that the helpers are used inside a form and generate the proper data-val attributes:
#{
this.ViewContext.FormContext = new FormContext();
}
Basically the Html.* helpers are generating data-val clients side validation attributes only if they are placed inside an Html.BeginForm. Except that in your case I guess that this Html.BeginForm is in your parent view and not inside the partial, so the #Html.DropDownListFor doesn't emit any validation attributes. By setting the current FormContext to a new instance in the partial as shown previously, the helper will generate the proper client side validation attributes on the corresponding input field.
If I already created a bunch of views is there any way to change their layout pages?
You can change the default layout page for the whole application in the file _ViewStart.cshtml. By default it has the the following code:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
This specifies that if any of your views do not explicitly state which layout to use they will use this layout.
Or, you can add code to your views (like above) that explicitly states which layout to use.