Sunday, July 25, 2021

What are the Main Razor Syntax Rules?

 Answer

  • Razor code blocks are enclosed in @{ ... }
  • Inline expressions (variables and functions) start with @
  • Code statements end with semicolon
  • Variables are declared with the var keyword
  • Strings are enclosed with quotation marks
  • C# code is case sensitive
  • C# files have the extension .cshtml

C# Example

<!-- Single statement block -->
@ {
    varmyMessage = "Hello World";
}
<!-- Inline expression or variable -->
< p > The value of myMessage is: @myMessage < /p>
    <!-- Multi-statement block -->
@ {
    var greeting = "Welcome to our site!";
    varweekDay = DateTime.Now.DayOfWeek;
    vargreetingMessage = greeting + " Here in Huston it is: " + weekDay;
} < p > The greeting is: @greetingMessage < /p>

What are HTML helpers in MVC?

 Just like web form controls in ASP.NET, HTML helpers are used to modify HTML. But HTML helpers are more lightweight. Unlike Web Form controls, an HTML helper does not have an event model and a view state.

In most cases, an HTML helper is just a method that returns a string. 

With MVC, you can create your own helpers, or use the built in HTML helpers.


Standard HTML Helpers

HTML Links

The easiest way to render an HTML link in is to use the HTML.ActionLink() helper.With MVC, the Html.ActionLink() does not link to a view. It creates a link to a controller action.

ASP Syntax

<%=Html.ActionLink("About this Website", "About")%>
ASP.NET (C#)

The first parameter is the link text, and the second parameter is the name of the controller action.

The Html.ActionLink() helper above, outputs the following HTML:

<a href="/Home/About">About this Website</a>
Markup

The Html.ActionLink() helper has several properties:

  • Property Description.
  • .linkText The link text (label).
  • .actionName The target action.
  • .routeValues The values passed to the action.
  • .controllerName The target controller.
  • .htmlAttributes The set of attributes to the link.
  • .protocol The link protocol.
  • .hostname The host name for the link.
  • .fragment The anchor target for the link.


HTML Form Elements

There following HTML helpers can be used to render (modify and output) HTML form elements: 

  • BeginForm()
  • EndForm()
  • TextArea()
  • TextBox()
  • CheckBox()
  • RadioButton()
  • ListBox()
  • DropDownList()
  • Hidden()
  • Password()

Thursday, July 22, 2021

What is the difference between View and Partial View?

 View:

=> It contains the layout page

=>Before any view is rendered, viewstart page is rendered

=>View is not lightweight as compare to Partial View

Partial View:

=> It does not contain the layout page

=> Partial view does not verify for a viewstart.cshtml. We cannot put common code for a partial view.

=> We can pass a regular view to the RenderPartial method




How can you navigate from one view to other view using a hyperlink?

 By using "ActionLink" method as shown in the below code. The below code will make a simple URL that helps to navigate to the "Home" controller and invoke the "GotoHome" action.


<%= Html.ActionLink("Home", "Gotohome") %>

What is the difference between adding routes to a webform application and an MVC application?

To add routes to a webform application, we use MapPageRoute() method of the RouteCollection class, while for adding routes to an MVC application, we use MapRoute() method.