I've implemented Entity Framework into my project, since I need to insert some data via a form to a custom table in my SQL database.
I've scaffolded all the CRUD views and have a button that links to the create view. This all works well and fine, but I need the create view to use the Master template that contains my header, navigation and footer.
In my create view I need to import my model, but I am unable to both use the #model and the #inherit keywords in the same file, so I am unable to inherit the master template.
Is it not possible to use Entity Framework with Umbraco? Do I need to create a partial view or Macro?
If you want to use a master page you have to set it at the header of the page like this:
If the name of the master page is Master here is the code.
#{
Layout = "Master.cshtml";
}
If you are using a umbraco page and you want to pass some custom objects from your controller here is the code for that:
#inherits Umbraco.Web.Mvc.UmbracoViewPage<List<ViewModel>>
If you are using only custom logic from backend which is not related to umbraco dashboard itself you dont need a macro!
Remember to inherit from SurfaceController.
Here is another question which may tell you more in details how to create your model and pass it to the view.
Remember your model should iherit from 'RenderModel' and you have to use umbraco form to post data back to the server as well.
If you have anything else unclear just let me know!
You also may wish to consider the concept of a headless CMS. That way, your views, models or anything in your app won't be necessarily dependent on the classes of your CMS. There are numerous headless CMSes in the market. Although I work with one of the vendors, I really want you to choose to your own preferences.
Related
I need to create a single page site in ASP.NET MVC (or CORE) for a project to school which will consist of 3 parts 1. A summary of statistics based on data from the database 2. A detailed table containing the entire extract of users from the database 3. Ability to add usera for the database from a manual form and adding a user from the csv file.
I would like to ask if creating 3 components for each of the necessary parts and displaying them in the common view is a good idea? Or Is it better to create one viewmodel that will contain a list of all the data that I will need to use? Or do you have any tips and suggestions for the implementation of this project?
As for the database, it will be the usual SQL database created in the code-first approach. To connect to the database and operations performed on it, I would like to use the interface that will serve as the data access layer.
For all the suggestions, thank you very much. I present the example concept of the page below
My standard approach is to 'silo' logic and views by object/type. In this way, your controllers and views are simplified and specialized (they only need to know how to interact with one type of object). This approach also lends itself really well to code and view reuse within your web app.
So in your example design, I might be looking at...
Controllers:
HomeController
Method to serve up the main view that will contain your 4 distinct sections
StatisticController (what type of statistics? People?)
Method to serve up partial view containing statistics
PersonController
Method to serve up partial view containing full list of Person objects
Method to serve up partial view containing single-Person upload form
Method to serve up partial view containing csv Person file upload
Method to accept POST'ed single-Person upload
Method to accept POST'ed CSV Person file upload
Views:
(Home)
Main view that contains empty layout for the 4 distinct sections, each of which will ultimately be populated from a partial view with, perhaps, Html.RenderAction())
(Statistic)
StatisticPartial view that contains the stats
(Person)
PersonIndexPartial view that contains the full list of Person objects
PersonEditPartial view that contains the single-Person upload form
PersonEditBatchPartial view that contains the csv Person file upload form
That's a bit to take in. I suggest starting with the main form, and getting the general layout set up the way you want it. Then start plugging partial views into each of the 4 distinct sections, one at a time. The more you progress, the more things will start to click.
Well in my solution I have view models in a different project from the original MVC web application. There is a problem however, that when I click the view folder to add view, it will no longer be able to find the view models from dropdown list. Below is a screenshot that demonstrates what exactly the problem is:
Sure I have an alternative solution that I can just create an empty view and then manually write #model ViewModelProject.ViewModel on the top. This isnt an ideal workaround though as I end up losing the benefits of scaffolding in ASP.NET MVC. Is there a way to make the view models show if they are in a different project/assembly? Anyone knows how to achieve this?
I have the business login written in Application_BeginRequest method of Global.asax file where i get an html content from database and set the content at view using literal control.
Now i am migrating the project into .net core where i want the same thing to do as mentioned above. I have got the html content in the code using middleware but now i am trying to show the content on view but unable to do so.
Following is my model
#model string
<div id="content">#Model</div>
and i am returning
return View(html);
as a string.
My question is, i want to know the steps that how can i show the html content coming from database on view using .net core.
Got the solution. i was missing the model. I declared the model and then passed that model to the view and then used #Html.Raw(#Model.Property) at view to render the HTML.
Thanks #adem caglin.
I have two fields in my Shared/~Layout that my user can use to filter information in my entire WebSite. Basically it loads the partial before the #RenderBody, however this partial always is reloaded when the user is redirected to another view, and the values that were selected are lost.
#Html.Partial("_FiltrosTop")
#RenderBody()
I would like to know, if there is a way to keep the values that were selected. I'm using C#, MVC4, ASP.NET, Jquery and KnockoutJS.
To load the fields data, I'm currently using Ajax and Jquery.
I resolved the problem using Jquery-cookie.
https://github.com/carhartl/jquery-cookie
Thanks all.
I had used both the Partial View and also the Layout Concept in my Project i cannot able to differentiate. But what i am feeling is both doing the same work. Can anyone tell the brief idea about the Partial View and Layout and difference with example?
In addition to Josh's answer, my aweeeesomeee paint skills would like to draw you a picture that should explain all..
Admit it... you're in awe...
You see the header and footer... you could even have partial view's there too.
EDIT...
Layout
To give you a different example of why you use each component (layout / view / partial view), imagine that you own a website that has 100 pages in total, and lets say you want to update the design of your website, how are you going to do it?
Updating each page individually would drive me insane, because your replicating your code constantly for every single page, just to update your design.
This is what the Layout view helps you solve, you use the Layout view to create a template for all of your pages.
View
Using our existing scenario of 100 page website, each page is going to have content that is unique, the View allows us to display this content whilst using our template from the Layout.
Partial View
Now lets imagine that we allow our visitors to comment on our pages, each comment must look consistent, and behave exactly the same as all the other comments throughout our website... To achieve this, you would use a Partial View which would act as a template for the comments that you receive on your website.
The benefits of doing this is that you don't have to repeat your code everywhere, you only have to create one Partial View to render any comment.
Layouts allow you to generate a consistent look across your entire site. Think of them like Master pages of ASP.net.
What are Layouts?
You typically want to maintain a consistent look and feel across all
of the pages within your web-site/application. ASP.NET 2.0 introduced
the concept of “master pages” which helps enable this when using .aspx
based pages or templates. Razor also supports this concept with a
feature called “layouts” – which allow you to define a common site
template, and then inherit its look and feel across all the
views/pages on your site. - http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts
Partial views allow you to construct a view and render it inside of a parent view. For instance, say have a site that allows you to comment on an article. The section in which displays and allows a user to add a comment is a piece of reusable code that is inserted into all of the pages you wish the functionality to exist. What makes this important is that you can then modify that single partial view file to update every view that renders that partial instead of tracking down each page that implements that code individually.
Here is a Youtube Vid that helped me understand partial views rather well. https://www.youtube.com/watch?v=SABg7RyjX-4
edit: Additionally, the guy who created the linked vid has an entire library of playlists that may help a new MVC coders. He walks through a great deal of the MVC features with decent examples. https://www.youtube.com/user/kudvenkat
Non-technical explanation:
Layout is a foundation of the house, View is a single room in that house and PartialViews are windows in that room or sockets with electricity in walls.
To make it simple Here is my answer:
1)
A layout is something that we can include once in a single page
and we can use the same layout to any number of pages.
2)
A partial view is something that we can include the same content
any number of times in a single page(where it is required) and can
be used in any number of pages.