CSharp · Mvc
using System;
using System.ComponentModel.DataAnnotations;
namespace HelloWorldMvcApp
{
public class SampleViewModel
{
public int CustomerId { get; set; }
public string Nome { get; set; }
public string Tipo { get; set; }
public string NCM { get; set; }
public string Contabil { get; set; }
}
}
@model HelloWorldMvcApp.SampleViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- CSS Includes -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<style type="text/css">
.field-validation-error {
color: #ff0000;
}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h1>Customer table</h1>
@using (Html.BeginForm())
{
<div class="form-group">
<table id="customer" style="width: 100%;">
<tr>
<td class="CustomerId"><span>1</span></td>
<td class="Nome"><span>AAAAA</span></td>
<td class="Tipo"><span>TIPO</span></td>
<td class="NCM"><span>NCM1</span></td>
<td class="Contabil"><span>ContabilA</span></td>
<td><button class="update" type="button">
Update</button></td>
</tr>
<tr>
<td class="CustomerId"><span>2</span></td>
<td class="Nome"><span>BBBBB</span></td>
<td class="Tipo"><span>TIPO2</span></td>
<td class="NCM"><span>NCM2</span></td>
<td class="Contabil"><span>ContabilB</span></td>
<td><button class="update" type="button">
Update</button></td>
</tr>
</table>
</div>
}
<div>JSON result:</div>
<div id="target">Please click Update button</div>
<br/><br/>
<div class="alert alert-warning fade">
<img src="http://entechprod.blob.core.windows.net/dotnetfiddle/morpheus.jpg" style="max-width:100%;"/><br/><br/>
<strong><span class="alert-content"></span></strong>
</div>
</div>
</div>
<!-- JS includes -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$(function(){
$('.update').click(function (e) {
e.preventDefault();
var row = $(this).closest('tr');
var customer = {};
customer.CustomerId = row.find(".CustomerId").find("span").html();
customer.Nome = row.find(".Nome").find("span").html();
customer.Tipo = row.find(".Tipo").find("span").html();
customer.NCM = row.find(".NCM").find("span").html();
customer.Contabil = row.find(".Contabil").find("span").html();
console.log(JSON.stringify({ customer: customer }));
$.ajax({
type: "POST",
url: '@Url.RouteUrl(new{ action="UpdateCustomer", controller="Home"})',
data: customer,
// data: JSON.stringify(customer),
// contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
$('#target').empty();
$.each(result, function(i, idx) {
$('#target').append('<p>' + i + ': ' + idx + '</p>');
});
},
error: function (xhr, status, err) {
alert(err);
}
});
});
});
</script>
</body>
</html>
using System;
using System.Web.Mvc;
using System.Collections.Generic;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new SampleViewModel());
}
[HttpPost]
public ActionResult UpdateCustomer(SampleViewModel customer)
{
return Json(customer);
}
}
}