1.Add two number and display in a detail page
2.Click on a link and navigate to different page
@Html.ActionLink("Go back", "About")
@Html.ActionLink("Go back1", "About",new { aa=1})
4.Reusable component concept
-RenderPartial & Partial
-By using helper
-Child Action Methods
-Render Section
------------------------
RenderPartial vs Partial
------------------------
The Partial method returns the markup as a string, whereas Html.RenderPartial writes
html directly to the output stream.
While one can store the output of Html.Partial in a variable or return it from a method, one cannot do this with Html.RenderPartial.
RenderPartial should be used
RenderPartial performance wise is good
RenderPartial is used inline
partial view @Html.Partial("resusable")
<body>
@{ Html.RenderPartial("_Header"); }
@Html.Partial("_Sidebar")
<div class="container body-content">
@RenderBody()
</div>
@{ Html.RenderPartial("_Footer"); }
</body>
---------------
By using helper
---------------
The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates
https://weblogs.asp.net/scottgu/asp-net-mvc-3-and-the-helper-syntax-within-razor
@helper DisplayWishMessage()
{
if (DateTime.Now.ToString("tt") == "AM")
{
<h3>Have a good Day!!</h3>
}
else
{
<h3>Have a good Night!!</h3>
}
}
@DisplayWishMessage() -->call using this
If we wish to call a helper method written in another view we can absolutely do so by
@Viewpagename.DisplayWishMessage()
---------------------
Child Action Methods
---------------------
Child Actions are designed to be called from within views which gives it an advantage to be used as a reusable component inside views. To implement it, a controller method is to be created with attribute ChildActionOnly.
[ChildActionOnly]
public ActionResult FetchChildAction()
{
return View("ChildActionView");
}
Calling
-------
@Html.Action("FetchChildAction")
or
@{ Html.RenderAction ("FetchChildAction"); }
--------------
Render Section
--------------
@RenderSection is used for injecting content in the defined section. It allows you to specify a region in Layout.
https://www.completecsharptutorial.com/asp-net-mvc5/asp-net-mvc-5-renderbody-renderpage-and-rendersection-with-example.php
master page
-----------
<div style="background-color:rebeccapurple; color:antiquewhite; font-weight:bold">
@RenderSection("Note",false)
</div>
<!-- End of Content Body -->
<!-- Footer -->
<footer>
<h4>I am Footer.</h4>
<div style="background-color:red; color:aliceblue">
@RenderSection("Footer", false)
</div>
</footer>
Child page
----------
@{
ViewBag.Title = "DarkLayoutPage";
Layout = "~/Views/Shared/_DarkLayout.cshtml";
}
<h2>DarkLayoutPage</h2>
Hello CompShop Application. I am in RenderBody section because there is no named section for me.
@section Note
{
I am a RenderSectionArea.
}
@section Footer
{
I am Footer Section Areas.
}
<h2>Hello world</h2>
In order to check section exist or not
@if (IsSectionDefined("RightCrumbContentArea")) {
@RenderSection("RightCrumbContentArea")
} else {
<span>poo</span>
}
3.State mangement
-cookie
-session
-localstorage
-sessionstorage
-viewbag
-viewdata
-tempdata
-stongely typed view
-query string
-------
cookie
-------
Set
HttpContext.Response.Cookies.Append("user_id", "1");
Get
var userId = HttpContext.Request.Cookies["user_id"];
In order to add expiry time
---------------------------
using Microsoft.AspNetCore.Http;
CookieOptions cookieOptions = new CookieOptions();
HttpContext.Response.Cookies.Append("first_request", DateTime.Now.ToString(), cookieOptions);
-------
session
-------
var products=Db.GetProducts();
Session["products"]=products //set
var products=Session["products"] as List<Product>; //get
Session["products"]=null; /clear
------------
localstorage
------------
In order to access in cs code
-----------------------------
string script = string.Format("sessionStorage.userId= '{0}';", "12345");
ClientScript.RegisterClientScriptBlock(this.GetType(), "key", script, true)
---------
viewdata
---------
The ViewData in ASP.NET MVC is a mechanism to pass the data from a controller action method to a view.
Employee employee = employeeBL.GetEmployeeDetails(102);
ViewData["Employee"] = employee;//set
<h2>@ViewData["Employee"]</h2>//get
--------
viewbag
--------
https://dotnettutorials.net/lesson/asp-dot-net-mvc-viewdata/
The ViewBag in MVC is one of the mechanisms to pass the data from a controller to a view.
ViewBag.Employee = employee;//set
<h2>@ViewBag.Header</h2>//get
The best and preferred approach in MVC to pass data from a controller action method to a view is by using a strongly typed model.
---------------------
stongely typed view
---------------------
@model FirstMVCDemo.Models.Employee
<h1>@Model.EmployeeId </h1>
ek view ma do forms kase add karte ha..
---------------------------------------------
https://stackoverflow.com/questions/4764011/multiple-models-in-a-view
This is the recommended approach
//Login.cshtml
@model ViewModel.LoginViewModel
@using (Html.BeginForm("Login", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Email)
@Html.PasswordFor(model => model.Password)
}
//register.cshtml
@model ViewModel.LoginViewModel
@using (Html.BeginForm("Login", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Email)
@Html.PasswordFor(model => model.Password)
}
@{Html.RenderPartial("login", Model.LoginViewModel)}
@{Html.RenderPartial("register", Model.RegisterViewModel)}
---------------------------------------------------------------------
OR
public class LoginViewModel
{
public string Email { get; set; }
public string Password { get; set; }
}
public class RegisterViewModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public BigViewModel
{
public LoginViewModel LoginViewModel{get; set;}
public RegisterViewModel RegisterViewModel {get; set;}
}
@model BigViewModel
@using(Html.BeginForm()) {
@Html.EditorFor(o => o.LoginViewModel.Email)
...
}
-------------------------------------------------------------------
Query string
------------
/Home/QueryTest?name=Jenna Doe
@Html.ActionLink("Go to details", "PersonDetails",new { name=1})
var name=HttpContext.Request.Query["name"];
6.antiforgerytoken
-------------------
Basically the anti forgery tokens stop anyone from submitting requests to your site that are generated by a malicious script not generated by the actual user.
There is an HTTP only cookie (not readable by a script running in the browser, but sent by the browser and accessible by the server) that gets sent to the client, it is used to generate a hidden field value which is then validated against the cookie.
Comments
Post a Comment