Thursday, June 20, 2013

MVC 4 Razor Quick Learning Book


Sr.No
Code Type
Syntax
1
Looping
@for(var i = 0; i < 10; i++){
  This is record @i
}
@foreach(Order order in OrderCollection) {
  @order.Cost
  @order.Quantity
}
 
 
2
Html Decode
@Html.Raw("

b>santosh

")
3
@Escape
 
We met @@time sharp!
4
Function
@functions{
  public bool isFooBar(string foo, string bar){
    return foo == bar;
  }
}
@{ var foo = "black";
  if(isFooBar(foo,"black")){
    Yup
  }
}
 
3
Switch Case
@switch(condition){
  case 1:
    @item.BodyText
    break;
  case 2:
    @item.Children.First().BodyText
    break;
  default:
    break;
}
 
4
Condtional
@if(item.HasValue("bodyText"){
  @item.BodyText
}else if(item.IsNull("bodyText")){
  this item is null
}else{
  Some other text
}
 

MVC Ajax JQuery
If we suppose want to load view based on Request received through ActionResult Controller .Then we can do that using following symantics

Public ActionResult Index()
{
if(Request.IsAjaxRequest()==true)
{
return Partial("XYZView",Model);

}
else
{
return view(Model);
}
}
ViewBag Vs ViewData

viewBag

is more cleaner dictionary to store dynamic data object.


Controller :
public ActionResult Foo
{
    ViewBag.FirstName="santosh";
    ViewBag.LastName="Poojari";
} 
View

@{
    var firstName = ViewBag.FirstName;
    var lastName = ViewBag.LastName;
}


ViewData


Controller :
public ActionResult Foo
{
    ViewBag["FirstName"]="santosh";
    ViewBag["LastName"]="Poojari";
}

View
@{    var firstName .= ViewData["FirstName"];    var lastName = ViewBag["LastName"]; }

Model

@model MVC4.Customer
This is declared at the top of view to include or reference the model. This is viewModel binding.
Once we do that we allow Model keyword to act as strongly typed viewmodel.
so now we can access Customer objectproperty  using
Hello @Model.CustomerName !

Expression: Foreach



    @foreach(var item in Model.Products) {

  

  •    @item.Name1

       @item.Upc1

      
      

  • }


    @if (Model.Count() > 0){    
            You've got data!    

    }

    _ViewStart.cshtml

    This is a bootstrap file that is applicable to all views in the solution.
    @{    Layout = "~/Views/Shared/_Layout.cshtml";
    }
    @{  
      if (HttpContext.Current.User.Identity.IsAuthenticated)   
     {      
        Layout = "~/Views/Shared/_Layout.cshtml";    
     }   
     else    
     {        
        Layout = "~/Views/Shared/_AnonLayout.cshtml";      
     }
    }

    @Html.Partial- Partial View

    Just partial page details _logOnPartial.chtml

    @Html.Partial("_LogOnPartial")

    @Html.RenderPartial-Partial View

    It used to call Controller with input parameter from view. The return type of controller would be Render.PartialView(PartialViewName.chtml)
    View
    @Html.RenderPartial("PersonName", Model.Person);
    Controller
    public ActionResult PersonName(string person)
    {
        return Render.PartialView(_LoginPartial);
    }

    @Html.RenderAction- Partial View

    View
    @Html.RenderAction("ListPartialView", "ProjectControllerMethod", new {personId = Model.Person.Id});
    Controller

    public ActionResult ProjectControllerMethod(int personID)

    {

        return View()

    }

    @Html.Action

    @Html.ActionLink

    @Html.ActionLink("Home", "Index",
    "Home")

    @Url.Action

    @Html.Raw ("

    Introduction

    ")

    Declarative HtmlHelper

    @helper Highlight(String value){    <span style="background-color: yellow">@value</span>}
    @Highlight("Foo")
    @helper CalcAndFormat(int a, int b, Func<intint, int> calc){    var sum = calc(a, b);    

    if(sum % 3 == 0)    
     {       
     @Highlight(sum.ToString())       

    else 
    {        <span>@sum</span>    }   

    }


    @* Outputs “27” with yellow highlighting *@
    @CalcAndFormat(3, 9, (a, b) => a * b)
    @* Outputs “50” with no highlighting *@ 

    @CalcAndFormat(2, 4, (a, b) => a * b + 42)

    Note:Spark Engine is more powerful than Razor.

    Reference :http://www.code-magazine.com/article.aspx?quickid=1103041&page=5

    No comments :