What is MVC?

by

The Model-View-Controller is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.

MVC Components

 

Model

The Model component corresponds to all the data-related logic that the user works with. This can represent either the data that is being transferred between the View and Controller components or any other business logic-related data. For example, a Customer object will retrieve the customer information from the database, manipulate it and update it data back to the database or use it to render data.

View

The View component is used for all the UI logic of the application. For example, the Customer view will include all the UI components such as text boxes, dropdowns, etc. that the final user interacts with.

Controller

Controllers act as an interface between Model and View components to process all the business logic and incoming requests, manipulate data using the Model component and interact with the Views to render the final output. For example, the Customer controller will handle all the interactions and inputs from the Customer View and update the database using the Customer Model. The same controller will be used to view the Customer data.

Using the DropDownList with ASP.NET MVC Razor

by

السلام عليكم ,

في المقالة التالية سيتم شرح كيفية تركيب  

DropDownList with ASP.NET MVC  .

سيكون المثال عبارة عند جلب البيانات من   SQL Server  واظهارها في ASP.NET MVC Page   .

الجداول التالية في SQL Server 

Employees

Gender 

تعرف الحقول التاليه في C#  , كما هو مبين :

من اجل 

Employee Table  .

  Gender Table  .

 

تهيئة  EmployeeController  . من اجل العمل مع الداتا بير .

 

الشكل الذي سوف يظهر لدينا عند تشغيل المشروع  :

 

Code  GitHub

Total Number of Visitor ASP.NET MVC

by

 

will explain the code to get the total visitors website using Application variable.

this way depends on how many current sessions on your website.

So , First :

  • after Create an ASP.NET MVC website ,write the below code in Global.asax file.

  • In Application_Start() method declare the variable like below code.
  • Application["TotalofVisitor"] = 0;

  • Write the session_Start() method like below:

protected void Session_Start()  {        Application.Lock();        Application["TotalofVisitor"] = (int)Application["TotalofVisitor"] + 1;        Application.UnLock();  }  

Now to Your main Layout.cshtml or the View where you want to show the total count put the below code.

<p>Total Number of visitors: @ApplicationInstance.Application["TotalofVisitor"]</p>

 

Read Add AppSettings to the Web.config File

by

Web.config File - ASP.NET MVC

I am going to explain about  Add/Update appSettings  in the Web.config file and how you can configure them to use in the application.

The simple following method from MDSN help you to read  any Key you sent it .


 public string  ReadSetting(string key)
        {
          var appSettings = ConfigurationManager.AppSettings;
           string result = appSettings[key] ?? "Not Found";
           return result;
        }

Last code ReadSetting , Just give your Key As String , will return the Value , if your key not exist will return to you "Not Found" String .

 

Add Key:

the next code will Add new Key with Value you give , but if there is any key is exist same as you add , just will Update the Value .

 static void AddUpdateAppSettings(string key, string value)
        {
             var configFile = WebConfigurationManager.OpenWebConfiguration("~/");
              var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }

 

After Add/Update your key , late say you need to use the Value some where in you C# code ,

Just use  it like the following Code :

String Str =ReadSetting(YourKey);

The question , how to read it  at Razor ??

Same way how we wrote it ReadSetting Method with some change ,

First add  System.Configuration to your References ,add it to your page :

 @using System.Configuration;

@{ 
    string  _YourValue = ConfigurationManager.AppSettings["YourKey"];
 }
 

I wish you find this post useful .