Un ejemplo de como obtenerlo en jquery.
@section Scripts { @Scripts.Render("~/bundles/jqueryval") <script type="text/javascript"> $(document).ready(function () { var id =@ViewContext.RouteData.Values["id"]; alert(id); }); </script> }
@section Scripts { @Scripts.Render("~/bundles/jqueryval") <script type="text/javascript"> $(document).ready(function () { var id =@ViewContext.RouteData.Values["id"]; alert(id); }); </script> }
<p> @Html.ActionLink("Create New", "Create") </p> @*Inicio del canvas de la grafica*@ <div id="canvas-holder"> <canvas id="chart-area" width="300" height="300" /> </div> <table class="table">
@section Scripts { @Scripts.Render("~/bundles/jqueryval") <script src="~/Scripts/Chart.js"></script> <script> var pieData = [ { value: 300, color: "#F7464A", highlight: "#FF5A5E", label: "Red" }, { value: 50, color: "#46BFBD", highlight: "#5AD3D1", label: "Green" }, { value: 100, color: "#FDB45C", highlight: "#FFC870", label: "Yellow" }, { value: 40, color: "#949FB1", highlight: "#A8B3C5", label: "Grey" }, { value: 120, color: "#4D5360", highlight: "#616774", label: "Dark Grey" } ]; $(document).ready(function () { var ctx = document.getElementById("chart-area").getContext("2d"); window.myPie = new Chart(ctx).Pie(pieData); }); </script> }
public JsonResult grafica() { var query = from c in db.Categories select new { value = c.Products.Count(), label = c.CategoryName }; return Json(query, JsonRequestBehavior.AllowGet); }
@section Scripts { @Scripts.Render("~/bundles/jqueryval") <script src="~/Scripts/Chart.js"></script> <script> $(document).ready(function () { $.post( '@Url.Action("grafica", "Categories")' ) .done(function (data) { var ctx = document.getElementById("chart-area").getContext("2d"); window.myPie = new Chart(ctx).Pie(data); }) .fail(function (data) { console.log('error !!!'); } ); }); </script> }
public class colores { private int _id; private string _color; private string _highlight; public int id { get { return _id; } set { _id = value; } } public string color { get { return _color; } set { _color = value; } } public string highlight { get { return _highlight; } set { _highlight = value; } } public colores(int id,string color, string highlight) { _id = id; _color = color; _highlight = highlight; } } public class categorias { private int _value; private string _color; private string _highlight; private string _label; public int value { get { return _value; } set { _value = value; } } public string color { get { return _color; } set { _color = value; } } public string highlight { get { return _highlight; } set { _highlight = value; } } public string label { get { return _label; } set { _label = value; } } public categorias(int value, string color, string highlight, string label) { _value = value; _color = color; _highlight = highlight; _label = label; } }
public JsonResult grafica() { List<colores> col = new List<colores>(); col.Add(new colores(0,"#F7464A", "#FF5A5E")); col.Add(new colores(1,"#46BFBD", "#5AD3D1")); col.Add(new colores(2,"#FDB45C", "#FFC870")); col.Add(new colores(3,"#949FB1", "#A8B3C5")); col.Add(new colores(4,"#4D5360", "#616774")); var query = from c in db.Categories select new { value = c.Products.Count(), label = c.CategoryName }; List<categorias> Cat = new List<categorias>(); int id = 0; foreach (var row in query) { colores filacolor; filacolor = col[id]; Cat.Add(new categorias(row.value, filacolor.color, filacolor.highlight, row.label)); if(id==4) { id = 0; } else { id++; } } return Json(Cat, JsonRequestBehavior.AllowGet); }
@model IEnumerable<WebApplication2.Models.Categories> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> @*Inicio del canvas de la grafica*@ <div id="canvas-holder"> <canvas id="chart-area" width="300" height="300" /> </div> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.CategoryName) </th> <th> @Html.DisplayNameFor(model => model.Description) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.CategoryName) </td> <td> @Html.DisplayFor(modelItem => item.Description) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.CategoryID }) | @Html.ActionLink("Details", "Details", new { id = item.CategoryID }) | @Html.ActionLink("Delete", "Delete", new { id = item.CategoryID }) </td> </tr> } </table> @section Scripts { @Scripts.Render("~/bundles/jqueryval") <script src="~/Scripts/Chart.js"></script> <script> $(document).ready(function () { $.post( '@Url.Action("grafica", "Categories")' ) .done(function (data) { var ctx = document.getElementById("chart-area").getContext("2d"); window.myPie = new Chart(ctx).Pie(data); }) .fail(function (data) { console.log('error !!!'); } ); }); </script> }
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using WebApplication2.Models; namespace WebApplication2.Controllers { public class CategoriesController : Controller { private ApplicationDbContext db = new ApplicationDbContext(); public class colores { private int _id; private string _color; private string _highlight; public int id { get { return _id; } set { _id = value; } } public string color { get { return _color; } set { _color = value; } } public string highlight { get { return _highlight; } set { _highlight = value; } } public colores(int id,string color, string highlight) { _id = id; _color = color; _highlight = highlight; } } public class categorias { private int _value; private string _color; private string _highlight; private string _label; public int value { get { return _value; } set { _value = value; } } public string color { get { return _color; } set { _color = value; } } public string highlight { get { return _highlight; } set { _highlight = value; } } public string label { get { return _label; } set { _label = value; } } public categorias(int value, string color, string highlight, string label) { _value = value; _color = color; _highlight = highlight; _label = label; } } public JsonResult grafica() { List<colores> col = new List<colores>(); col.Add(new colores(0,"#F7464A", "#FF5A5E")); col.Add(new colores(1,"#46BFBD", "#5AD3D1")); col.Add(new colores(2,"#FDB45C", "#FFC870")); col.Add(new colores(3,"#949FB1", "#A8B3C5")); col.Add(new colores(4,"#4D5360", "#616774")); var query = from c in db.Categories select new { value = c.Products.Count(), label = c.CategoryName }; List<categorias> Cat = new List<categorias>(); int id = 0; foreach (var row in query) { colores filacolor; filacolor = col[id]; Cat.Add(new categorias(row.value, filacolor.color, filacolor.highlight, row.label)); if(id==4) { id = 0; } else { id++; } } return Json(Cat, JsonRequestBehavior.AllowGet); } // GET: Categories public ActionResult Index() { return View(db.Categories.ToList()); }
<system.webServer> <rewrite> <rules> <rule name="http to https" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:0}" /> </rule> </rules> </rewrite> </system.webServer>
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=localhost; Initial Catalog=northwind;user id=sa;password=P@ssw0rd" providerName="System.Data.SqlClient" /> </connectionStrings>
[Authorize] public ActionResult About() { return View(); } [Authorize(Roles = "administradores")] public ActionResult About() { return View(); } [Authorize(Users="user1")] public ActionResult About() { return View(); }
<form> <input type="text" name='xx' value="test"> </form> <script> $(document).ready(function() { var algo=$("input[name=xx]").val(); alert(algo); }); </script>
Por valor:
<form> <input type="button" value="Delete"> </form> <script> $(document).ready(function() { var algo=$("input[type=button][value=Delete]").val(); alert(algo); }); </script>
<form> <input type="button" class="" style="" value="Delete"> <input type="button" class="" style="" value="edit"> <input type="button"class="" style="" value="Delete"> <input type="button" class="" style="" value="Delete"> </form> <script> $('input[type=button]').each(function() { var btn= $(this); if(btn.val()=='Delete') { btn.hide(); } }); </script>
var query = (from c in db.customers where c.customerid == 1 select c).ToList().First();
try { var query = (from c in db.customers where c.customerid == 1 select c).ToList().First(); } catch (Exception) { throw; } 2.) Puedes cambiar tu "First" por un "FirstOrDefault" cuando la consulta no tenga registros dejara la variable query como nulo y no dará error.var query = (from c in db.customers where c.customerid == 1 select c).ToList().FirstOrDefault();
[HttpPost] [ValidateAntiForgeryToken] public JsonResult getcategories() { var query = from c in db.Categories select new { c.CategoryID, c.CategoryName }; return Json(query, JsonRequestBehavior.DenyGet); } [HttpPost] [ValidateAntiForgeryToken] public JsonResult getsuppliers() { var query = from s in db.Suppliers select new { s.SupplierID, s.CompanyName }; return Json(query, JsonRequestBehavior.DenyGet); }
public ActionResult Create() { //ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName"); //ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName"); return View(); }
@Html.DropDownList("SupplierID", String.Empty)
Por:
<select name="SupplierID" id="SupplierID"> <option value="">Seleccione uno...</option> </select>
Cambia:
@Html.DropDownList("CategoryID", String.Empty)
Por:
<select name="CategoryID" id="CategoryID"> <option value="">Seleccione uno...</option> </select>15.) En el mismo archivo "Create.cshtml" debemos agregar el Json de llenado, busque la sección "Script" y reemplace por los siguiente:@section Scripts { @Scripts.Render("~/bundles/jqueryval") <script> $(document).ready(function () { //var token = $('[name=__RequestVerificationToken]').val(); load_suppliers(); load_categories(); }); function load_categories() { var token = $('[name=__RequestVerificationToken]').val(); var data = { __RequestVerificationToken: token }; $.post( '@Url.Action("getcategories", "products")', data ) .done(function (data) { $.each(data, function (i, row) { var $option = $('<option>'); $option.val(row.CategoryID); $option.html(row.CategoryName); $('#CategoryID').append($option); }) }) .fail(function (data) { console.log('error !!!'); } ); } function load_suppliers() { var token = $('[name=__RequestVerificationToken]').val(); $.ajax({ url: '@Url.Action("getsuppliers", "products")', data: { __RequestVerificationToken: token }, type: 'POST', success: function (data) { $.each(data, function (i, row) { var $option = $('<option>'); $option.val(row.SupplierID); $option.html(row.CompanyName); $('#SupplierID').append($option); }) }, error: function (msg) { } }); } </script> }El resultado sera: