Remark :
Html.BeginForm() and Ajax.BeginForm()
Html.BeginForm()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; //List<SelectListItem> using System.ComponentModel.DataAnnotations; //RegularExpression namespace razor_and_xslt.Models { public class ProductModel { public ProductModel() { Category = new List<SelectListItem>(); Category.Add(new SelectListItem { Text = "Books", Value = "1" }); Category.Add(new SelectListItem { Text = "Mobiles & Tablets", Value = "2" }); Category.Add(new SelectListItem { Text = "Laptops & Accessories", Value = "3" }); } public int ProductId { get; set; } [Required] public string Name { get; set; } public string Description { get; set; } public string Manufacturer { get; set; } [RegularExpression(@"^\$?\d+(\.(\d{2}))?$")] public decimal BasePrice { get; set; } public IList<SelectListItem> Category { get; set; } public int CategoryId { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using razor_and_xslt.Models; //ProductModel namespace razor_and_xslt { public ActionResult ProductHtml() { ProductModel model = new ProductModel(); return View(model); } [HttpPost] public ActionResult ProductHtml(ProductModel model) { if (ModelState.IsValid) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("Product Name :" + model.Name + "</br/>"); sb.Append("Description :" + model.Description + "</br/>"); sb.Append("Manufacturer :" + model.Manufacturer + "</br/>"); sb.Append("Price :" + model.BasePrice + "</br/>"); sb.Append("Category :" + model.Category[model.CategoryId - 1].Text); return Content(sb.ToString()); } else { return View(model); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
@model razor_and_xslt.Models.ProductModel @using System.Web.Mvc.Html; @{ ViewBag.Title = "Product Master HTML"; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>ProductHtml</title> </head> <body> <div> <h2>Product Master</h2> <fieldset> <legend>Product</legend> @using (Html.BeginForm()) { <div class="formRowContainer"> <div class="labelContainer">Name</div> <div class="valueContainer"> @Html.TextBoxFor(model => model.Name) @Html.ValidationMessage("Name") </div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Description</div> <div class="valueContainer">@Html.TextBoxFor(model => model.Description)</div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Manufacturer</div> <div class="valueContainer">@Html.TextBoxFor(model => model.Manufacturer)</div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Price</div> <div class="valueContainer"> @Html.TextBoxFor(model => model.BasePrice) @Html.ValidationMessage("BasePrice") </div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Category</div> <div class="valueContainer"> @Html.DropDownListFor(model => model.CategoryId, Model.Category, "--Select--") </div> </div> <div class="clearStyle"></div> <div class="buttonContainer"> <button>Add Product</button> </div> } </fieldset> </div> </body> </html> |
Working With Ajax.BeginForm()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public ActionResult ProductAjax() { ProductModel model = new ProductModel(); return View(model); } [HttpPost] public ActionResult ProductAjax(ProductModel model) { if (ModelState.IsValid) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("Product Name :" + model.Name + "</br/>"); sb.Append("Description :" + model.Description + "</br/>"); sb.Append("Manufacturer :" + model.Manufacturer + "</br/>"); sb.Append("Price :" + model.BasePrice + "</br/>"); sb.Append("Category :" + model.Category[model.CategoryId - 1].Text); return Content(sb.ToString()); } else { return View(model); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
@model razor_and_xslt.Models.ProductModel @{ ViewBag.Title = "Product Master Ajax"; } <h2>Product Master</h2> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Product</title> <script src="@Url.Content("~/Scripts/jquery-3.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> </head> <body> <div> <fieldset> <legend>Product</legend> @using (Ajax.BeginForm("ProductAjax", "Home", new AjaxOptions { UpdateTargetId = "Productresult" })) { <div id="Productresult"></div> <div class="formRowContainer"> <div class="labelContainer">Name</div> <div class="valueContainer"> @Html.TextBoxFor(model => model.Name) @Html.ValidationMessage("Name") </div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Description</div> <div class="valueContainer">@Html.TextBoxFor(model => model.Description)</div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Manufacturer</div> <div class="valueContainer">@Html.TextBoxFor(model => model.Manufacturer)</div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Price</div> <div class="valueContainer"> @Html.TextBoxFor(model => model.BasePrice) @Html.ValidationMessage("BasePrice") </div> </div> <div class="clearStyle"></div> <div class="formRowContainer"> <div class="labelContainer">Category</div> <div class="valueContainer"> @Html.DropDownListFor(model => model.CategoryId, Model.Category, "--Select--") </div> </div> <div class="clearStyle"></div> <div class="buttonContainer"> <button>Add Product</button> </div> } </fieldset> </div> </body> </html> |