Getting Data through View Model in C# ASP.NET MVC _Layout view - c#

I am Using ASP.NET C# MVC.
I have a Table in Database Where I kept Layout Settings (Site name , Header text etc).With Log in I want to Show this on Appropriate place in _Layout view.
What I am doing & want is I want to have a "View Model" Bind in _layout view With some Data (data comes from a action => service layer => data Layer).
Where i should give hand ?? what can I do ?

Do you want to define a _layout page for you application which should have fixed sections like header, footer and then a middle section where different pages can populate them self. There are many examples on the web related to this. Search on Google "ASP.NET MVC layout pages". If you have any specific problem implementing any of those solutions, please post the question and people should be able to answer those.

Related

creating many instances of a webpage from one template?

I'm creating a new website application in asp.net. The landing page needs to have a button (or something similar) which the user can click to create a new instance of a webpage. Similar to how a Facebook user can create a new group/event or a StackOverflow user create a new question.
My website needs to be able to create multiple "events" from the landing page which can then be accessed from the landing page, each event should be a template populated with user details on creation.
Can someone please tell me how people refer to this technique of creating many instances of a webpage (event) from one template?
With ASP.NET Core MVC (using this as an example as you have an ASP.NET tag and your description doesn't specify a technology), you can create a template using a .cshtml file. If you are not familiar with these types of files (which are used within the ASP.NET framework), then I suggest a read of it here:
https://www.w3schools.com/asp/razor_syntax.asp
Roughly, it's a file with HTML content where you can easily embed .NET types (such as types from your Model) and .NET logic using "Razor syntax", so that your HTML file is modified appropriately (e.g. with queried data specific to your user) before being sent back to the client. The reference above gives good examples, so I'm not going to waste space and repeat them here.
You can have certain .cshtml files as your "template" and embed appropriate model data using Razor syntax. You can then have a hyperlink tag (for example) reference the .cshtml file using the asp-action attribute. This will render the .cshtml file to the client whenever that tag is clicked on. ASP.NET uses types called Controllers to handle such requests (Controllers are types that inherit from the Controller type) appropriately, such as querying the correct database and providing your .cshtml file with the correct data before sending the result back to the client.
ASP.NET Core MVC modularizes the types of actions described above very well (M --> Model, V --> View, C --> Controller). Here is a good reference:
https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2
For other technologies that you wish to use to achieve the same result, you will have to consult the appropriate references.
Stackoverflow is a place to get answer on specific problem with short answer. Your problem is general design and programming question, and requires understanding of basic programming aproaches.
For that you should grab a book and read about designing webapplications in .Net.

Location the anchor tag for Home, About and contact buttons

I am new to ASP.net MVC web development. I created the default MVC application! In the top of the Home page You find Home,About and contact buttons.
But When I checked the index.cshtml I can not find the code for Home,About and contact buttons!
I searched other files in the solution but was very hard to locate them! In which file can I find the code for these buttons?
yes..kushan....that is why it is suggested in mvc that to start with
empty mvc application
instead of going for the ready made template ..
Because by manually creating each part you would learn the use of layout , partialview , how to add scripts & webconfig , startup.cs etc
anyways the corresponding code for your landing page
could be found in the respective controller & views folder
Your looking for asp.net-mvc Layouts.
Excerpt:
Most web apps have a common layout that provides the user with a consistent experience as they navigate from page to page. The layout typically includes common user interface elements such as the app header, navigation or menu elements, and footer.
While adding view to your project it show option to include layout page,
ADD LAYOUT PAGE.
By default c# mvc application contains layout.cshtml page which contain your about, home etc links.
So by enabling layout.cshtml to your page help you out.
If you want to add it manually then create anchor tag to your page which contain 'href' that contain links.Also make methods in controller to handle link clicks.
You can also add your own layout.cshtml page which contain link to your home,about, contact us etc and refer layout.cshml to every other cshtml page.

Customized View + Update Form using ASP.NET MVC5

I am new to ASP.NET MVC5 and already familiar with adding a scaffold item using MVC5 Controller with views using the Entity Framework.
Is there a way I can customize the web pages like this:
where I can split the existing records (first tab has 5, second has 4, and so on...) from the database on different tabs and update the values once.
Any suggestions?
You can have a master view that gets partial view data via AJAX. Each partial view could have its own model. That way you can split the model logically into many views.
Once the values are changed you can post changes via JavaScript. You can use KnockoutJS to track changes to the model on the client side.
You can use Jquery UI Tabs to display the tabs. cut and paste the razor markup generated by the visual studio to the respective tabs

Accessing a MVC3 view via absolute URL without displaying it

What I try to achieve:
Convert a MVC3 view to PDF with abcpdf 8.
It is a result view where I want to cut off party of the views DOM like navigation and other website related parts.
Abcpdf needs, in order to render the html with all css formattings correctly, a absolute url. But I don't want to show the result-pdf view to the user. So how can I access a view from controller by URL without displaying the view. The original result view shall be displayed all the time.
Thank you in advance
Christian
You cannot access a View directly with MVC, you have to go through a Controller method. In ASP.NET MVC, URLs map to Controller methods, not to Views.
So if you know the URL of the Controller method that calls the View, you can take that URL and pass it to ABCPDF.

Adding custom code/script into header files in ASP.net MVC 3 application?

I have an MVC application and i'm trying to make one of the views display this editable table and have server side code to actually support the javascript based editor with the appropriate callback functionality.
My first problem is that I need to add some custom javascript to the header of the view the table should appear in but it looks like the header is confined in _Layout.cshtml which means adding it there would make it in every view in the MVC app.
Anyone know how I can just add stuff to the header file only in the view controller I want it to be in?
If you need detail
In _layout.cshtml header section add this line
#RenderSection("HeaderJS",false)
In view use this section as follow
#section HeaderJS{
// Write your script or css inclusion over here.
}
Create a Section in your Layout that's in the head. The views can then add things to that section. See ScottGu's discussion of this: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

Categories