Monday 9 January 2017

JavaScriptResult Class

The JavaScriptResul class implements the abstract ActionResult class and is used to sends JavaScript content to the response.

Example:
1)
    Somewhere in Home controller:
              public JavaScriptResult RunJS() {
        var script = "alert('Javascript is running');";
        return new JavaScriptResult() { Script = script };
       }
       and somewhere in a some view:
       <script>
         $(document).ready(function () {
             $("button").click(function () {
                $.getScript("/Home/RunJS");
             });
          });
        </script>

        <button>Use the button to run a JavaScript</button>
2)
   Somewhere in Home controller:      
          public JavaScriptResult RunJS() {
                var script = string.Format(@"numberOfProducts = 135;",;
                return new JavaScriptResult() { Script = script };
          }
           and somewhere in a some view:
          <script type="text/javascript" src="/Home/RunJS">
           if(numberOfProducts >= 100)
             {
               alert('Javascript is running');             }
          </script>

Saturday 7 January 2017

PartialViewResult Class

The PartialViewResult class implements the abstract ViewResultBase class and is used to render a partialview.
.
Examples:
           
           1)    Passs data to PartialView using Model
                    public PartialViewResult Index()
            {
                return PartialView("_NameOfPartialView", Model);

            }
     2)  
          public PartialViewResult Index()
           {
                return PartialView("_NameOfPartialView");

           }

Thursday 5 January 2017

ViewResult Class

The ViewResult class implements the abstract ViewResultBase class and is used to render a view.
By default, ASP.NET MVC checks first in \Views\[Controller_Dir]\, but after that, if it doesn't find the view, it checks in \Views\Shared.
Examples:
            1)        
                  public ActionResult Index()
           {
                return View();
           }

           2)    Passs data to View using Model
                    public ActionResult Index()
            {
                return View("NameOfView", Model);

            }
     3)  
          public ActionResult Index()
           {
                return View("NameOfView");

           }
     4) Passs data to View using ViewData
         public ActionResult Index()
          {
            ViewData["somedata"] = "%43***&FGTRRR";
            return View();
          }
     5) Display a view from another controller
         public ActionResult Index()
           {
            return View("~/Views/ControllerName/NameOfView")
           }

Labels: ,

Tuesday 3 January 2017

ActionResult and derived classes

ActionResult is the base return type of Action Method.
The following table shows the classes which are  derived from ActionResult Class.

  1. ViewResult
  2. PartialViewResult
  3. RedirectResult
  4. RedirectToRouteResult
  5. ContentResult
  6. JsonResult
  7. JavaScriptResult
  8. FileResult
  9. FileContentResult
  10. FilePathResult
  11. FileStreamResult
  12. EmptyResult
  13. HttpNotFoundResult
  14. HttpUnauthorizedResult
  15. HttpStatusCodeResult


In some controller the general structure of Action method is as the following:
  1. public ActionResult ActionMethodName()
    {
        return {new} SomeHelperMethodRelatedToDerivedClass();
    }

Labels: ,

Monday 2 January 2017

Web based MVC and Observer design pattern

The observer pattern was first implemented in Smalltalk's MVC based user interface framework
Due to the stateless "nature" of web programming the observer pattern is not built-in  in ASP.NET MVC architectural pattern although is implemented  in .NET Framework explicitly (IObserver,IObservable). 

What type of pattern is the MVC pattern?

In classic notion of term (Gang of Four) MVC is not a design pattern.
MVC is an Architectural pattern focused on the UI / interaction layer of an application.

Wednesday 28 December 2016

MVC Anatomy


"in our image and after our likeness"