Error with with namespace referencing in asp.net mvc - c#

Hello all am a freshman to MVC 5 and am trying to achieve a project by following a particular video tutorial. In this tutorial I was asked to create folders in Area for Users, Admin, Common and Security. We added BOL using database first Approach. in other to interact with this BOL we added a controller class in Users controller folder and name It index for us to create a get form for submitting details with form. this approach lead to scaffolding template for getting a form. Now at the point of running my project to see the form created an error is being displayed..
My Index.cshtml is as follow:
[Controller][3]
[Folder Arrangement in area][4]

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.

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

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.

WebMatrix - Create controller for CSHTML page

I'm working on a website with WebMatrix. I recently asked a question about fetching data from a database. When I had tried writing the data logic within my CSHTML page, I was quickly told that I was in the wrong to do that because I was contradicting the principals of MVC. The problem is, MVC was easy for me in Visual Studio 2010, where I had a view page.aspx and a controller page.aspx.cs for each page, but in WebMatrix, I am using the Razor C# syntax, so my pages are only a single page.cshtml file. When I create a new site or a new file, there is no MVC template, so I have to create the views and controllers myself.
How can I display data in page.cshtml that is fetched from the database using a controller (would my controller be 'page.cshtml.cs'?)? In other words, how can I separate a pages data logic from its actual content/markup/view?
I think you are confusing ASP.NET MVC with WebMatrix WebPages. You are using only WebPages where all the code is inside Razor templates. There are no notions of controllers.
But even in WebMatrix you could separate your data access code in a separate file. Create a special folder called ~/App_Code and inside this folder you could create .NET classes. For example you could have a DataAccess.cs file where you could put your data access logic. You could also reference external assemblies that you have created. For example:
#using MyNamespace
#{
DataAccess myClass = new DataAccess();
...
}

MVC from scratch

I don't use the ready made MVC template by VS10, I create an empty web application then create folders: Models, Views, Controllers and would like to create a simple MVC web application from scratch, I don't find any tutorial about this online, I have created the DB for my application, I need what next steps I should do to make a login/logout sessioned web application runnable on the browser.
Phil Haack has a really empty MVC project template http://haacked.com/archive/2012/01/11/a-really-empty-asp-net-mvc-3-project-template.aspx. The bare minimum.
As for authentication why can't you use the default project template which has a working example. If you really don't want to use it, search online. Lots of examples of how to implement authentication. If you have a specific problem with authentication, ask another question here.
The really empty MVC project template is a good start. I'd nuget the jquery, jquery.validation also.
About authentication , I'll give you some hints to research: FormsAuthenticationTicket, FormsAuthentication.Encrypt/Decrypt, Response.Cookies, FormsAuthentication.SignOut (sic), asp.net pipeline events

MVC can't find my Areas page

I created an Area in my application named Admin. It's pretty basic, looks like this:
Areas
Admin
Controllers
CompaniesController.cs
Models
Views
Companies
Index.aspx
AdminAreaRegistration.cs
When I try to view my Index page by going to /Admin/Companies, I get a "The resource cannot be found" message. I find it weird that I don't get any sort of error message.
I'm not sure what's going on here, has anyone come across this?
If you newly created Index.aspx, you need to "REBUILD" the solution or your web project. Build only compile your last changes and rebuild build everything from the scratch. I faced that problem when I started writing Asp.net MVC project.
Does it solve your problem?

Categories