How to use model in _Layout page in Asp.Net core - asp.net-core-mvc-2.1

I want to use Model in my _Layout page because I want to make my layout Title Logos and footer dynamic through the data table here the question is how could I use that model and how can I create a controller that will be work on _layout view!

Related

Recommended way to do the search and populating the grid in ASP.NET Core MVC 6

I am creating a project where I need to create a view with search textboxes and a dropdown which would be populated from the server. Same view also have a grid which would be populated based on the search criteria enter/selected by user.
I want to know what would be the design of the page in terms of showing both on same page. Should I create a partial view for the grid or Search panel or add both in the single view?
Note that dropdown list would need to be populated from the ViewModel. So what is the common practice in the situation. I am new to this I have done few pages but with lot of code, session and ViewBags and I think I am not following recommended practice.
Actually, the design for your web application is according to your requirement.
For example, if you want grid to work with other datasource or view, you could build it as a component, then if you want to use it, you could directly use this component to avoid write the same codes for multiple time.
If you want to use same grid to show in some other page, you could build it a partial view, then you could directly call this view to show something.

How to position a partial view in a page when load dynamically after postback

i am new in mvc so like to ask 2 noob questions and those are
1) how to load partial view dynamically after postback. suppose i have a button in my main view and when user will click then i want to load a partial view dynamically after postback. i know how to load partial view dynamically using jquery but i want to know how can i load partial view after postback in main view without using jquery.
2) when we load any view dynamically by jquery then very easily we can capture the view rendered html and set or position that html in any where in my main view but i want to know how could i position or adding partial view html after postback. is it at all possible without using jquery?
in asp.net webform we can load user control dynamically after postback and also we can add that usercontrol any where in page without using jquery. the same kind of flexibility is there in mvc or is it restricted.
please give answer point wise with example or sample code. thanks
for your first question you can use different techniques, here is a sample
[HttpPost]
public ActionResult Foo()
{
ViewBag.ShowPartial = true;
return View();
}
in the view
#if (ViewBag.ShowPartial)
{
Html.Partial("_MyPartial");
}
for second question i suggest you read a little more about asp.net mvc, and postback means nothing in asp.net mvc, it is post request and get request so on, and you can easily add what ever you want to the view
one other sample
in the view
#if (Request.RequestType == "Post")
{
// do what ever you want
}

Both create and list in the same view MVC4?

I am new to MVC4. I know basic CRUD functionality in MVC, but how can I achieve both create and list in the same view? Like after a user creates data the user will automatically be redirected to the same view and view the list of data.
You may use partial views to accomplish this. Create the create / edit pages the same way as you do and below the form you may render a partial view that would render the list with even edit / delete buttons.
Alternatively, you may pass both the form data (in case of edit) and the list data to a view to render there.
I would suggest you to use the partial views approach as it would simplify the view and the partial view can be reused on some other page also.
Please take this as a starting point and not as a follow-it-blindly solution.

ASP.NET MVC loading multiple partial views into a single div using JQuery

I am working on a help page which is composed of a navigation tree, content box, and search box. The navigation tree has links to Frequently Asked Questions and Glossary, each of which are linked to an Action which return partial views.
To the right of the navigation tree I have a single content div that I would like to contain whichever partial view is selected in the navigation tree.
Success case (what I want to accomplish): clicking on one of the FAQ links calls the FAQ() method in my controller, then returns a partial view which is then displayed in my content div to the right of the navigation tree. Clicking on another item would cause another partial view to be loaded.
How do I go about this? I've read a ton of ASP.NET MVC JQuery loading blog posts and tutorials but can't find anyone who's done exactly this.
Many thanks!
You should be able to use the jQuery method .load() to load HTML into your div.
http://api.jquery.com/load/
You can create an action that returns the partial view as HTML.
ASP.NET MVC Returning Partial View as a full View Page
jQuery:
one easy way you can do is load all partial views in "Container Div" at page load in one go (if performance is not a issue)
then assign each partial div with different div id inside "container", than use jquery to control show(); hide(); for each div.
MVC:
however if i were you, "Glossary" and "FAQ" looks same model to me it shouldn't be put in different partial view in first place.
if they did designed in separate model, in this scenario, i would recommend you to create a proxy class as a ViewModel above models you want to display, and then load it with one partial view only

What is the difference between a View and a PartialView in ASP.NET MVC?

What is the difference between a View and a PartialView in ASP.NET MVC?
At first glance the need for both seems non-obvious to me.
In theory, the answer is: A partial view is a "sub-view" that you embed within a main view - something that you might reuse across multiple views, like a sidebar.
In practice, the answer is: Very little.
In theory, partial views are more lightweight than standard views, but it's perfectly OK to pass a "regular" view to RenderPartial and the performance seems to be exactly the same. I frequently use regular .aspx views as "partial" views because you can make them reference a master view in order to provide templated content like what you can do with UserControls in ASP.NET WebForms. See here.
Partial views are more like web parts on a portal - they are completely self-contained objects. Use them if the layout is simple and static, or if you're annoyed by the Intellisense errors when you don't have the <html> and <body> tags in a standard View.
It works like that:
return View() the view content goes in the #RenderBody() of the /Shared/_Layout.cshtml
return PartialView() it returns only the view content
Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.
If you're coming from webforms, view is similar to a web content form, while a partial view is like a user control.
If you come from a webforms background, think of PartialView as a usercontrol.
Look at StackOverflow.com site:
Main site (View) contains components like:
Tags
Related
Ad
So Tags, related, Ad etc. can be composed as PartialViews. The advantage of this is that
PartialViews can be simply cached by OutputCache instead of recreating all site: performance gain.
Consider a partialview like a control in webforms, the idea is the partial is reusable
View in Core Mvc
View Contains The layout Page(_ViewStart) Or Layout Property.
#{
Layout="_Layout"; }
Before Any View Rendered _ViewStart is Rendered.
View might have markup tags As
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
View isn't Lightwight as Partial View.
View will Render in Layout by used #RenderBody() Method
(Views/Shared/_Layout.cshtml).
Calling by use return View() inside Action.
Partial View
Partial view doesn't Contain Layout Page Layout Property
It does not verify for a viewstart.cshtml. We cannot put common code
for a partial view within the viewStart.cshtml.page.
In MVC Partial view is designed specially to render within the view
We can pass a regular view to the RenderPartial method.
#{await Html.PartialAsync("Partial"); }
Example To Demo Partial View Is Strongly Type
in View
#model List<Student>;
#{
Layout = "_Layout";
}
<div>#await Html.PartialAsync("Partial",Model)</div>
in Partial View
#model List<Student>; <ul>
#foreach (Student student in Model)
{
<li>#student.ID</li>
<li>#student.Name</li>
<li>#student.Age</li>
}
</ul>
SP.NET Core MVC looks for Partial View in the same way like Regular
Views (i.e. in Views/ and Views/Shared folders). This
means you can create controller specific partial views or shared
partial views.
Good Luck

Resources