1 | replace(comment,"<br/>",vbcrlf> |
在MVC中,因為是使用Razor語法,要換行要這樣寫
1 | <span style="white-space: pre-line">@Html.DisplayFor(modelItem => item.Comment)</span> |
1 | replace(comment,"<br/>",vbcrlf> |
在MVC中,因為是使用Razor語法,要換行要這樣寫
1 | <span style="white-space: pre-line">@Html.DisplayFor(modelItem => item.Comment)</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function AddData() { var product = { Id: document.getElementById('txtId').value, Name: document.getElementById('txtName').value, Category: document.getElementById('txtCategory').value, Price: document.getElementById('txtPrice').value }; $.ajax({ url: '/api/Products/PostProduct', type: 'POST', data: JSON.stringify(product), contentType: "application/json;charset=utf-8", success: function (data) { alert('新增成功'); GetData(); }, error: function () { alert('新增失敗'); } }); } |
1 2 3 4 5 | <input type="text" id="txtId" value="" /><BR /> <input type="text" id="txtName" value="" /><BR /> <input type="text" id="txtCategory" value="" /><BR /> <input type="text" id="txtPrice" value="" /><BR /> <input type="button" id="btnAdd" value="新增資料" onclick="AddData();" /> |
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 | @section Scripts { <script type="text/javascript"> $(document).ready(function () { $.getJSON("/api/Products", function (result) { var strHtml = ""; strHtml += "<table>"; for (var i = 0; i < result.length; i++) { strHtml += "<tr>"; strHtml += "<td>"; strHtml += result[i].Id; strHtml += "</td>"; strHtml += "<td>"; strHtml += result[i].Name; strHtml += "</td>"; strHtml += "<td>"; strHtml += result[i].Category; strHtml += "</td>"; strHtml += "<td>"; strHtml += result[i].Price; strHtml += "</td>"; strHtml += "</tr>"; } strHtml += "</table>"; $("#Content").html(strHtml); }) .error(function (res, textStatus, errorThrown) { alert("error ==> " + res.responseText); }); }); </script> } <p/> <div id="Content"></div> |
1 | GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
|
1 2 3 4 5 6 7 | public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } } |
1 2 3 4 5 6 7 8 | public interface IProductRepository { IEnumerable<Product> GetAll(); Product Get(int id); Product Add(Product item); void Remove(int id); bool Update(Product item); } |
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 | public class ProductRepository : IProductRepository { private List<Product> products = new List<Product>(); private int _nextId = 1; public ProductRepository() { Add(new Product { Name = "Tomato soup", Category = "Groceries", Price = 1.39M }); Add(new Product { Name = "Yo-yo", Category = "Toys", Price = 3.75M }); Add(new Product { Name = "Hammer", Category = "Hardware", Price = 16.99M }); } public IEnumerable<Product> GetAll() { return products; } public Product Get(int id) { return products.Find(p => p.Id == id); } public Product Add(Product item) { if (item == null) { throw new ArgumentNullException("item"); } item.Id = _nextId++; products.Add(item); return item; } public void Remove(int id) { products.RemoveAll(p => p.Id == id); } public bool Update(Product item) { if (item == null) { throw new ArgumentNullException("item"); } int index = products.FindIndex(p => p.Id == item.Id); if (index == -1) { return false; } products.RemoveAt(index); products.Add(item); return true; } } |
1 | static readonly IProductRepository repository = new ProductRepository(); |
1 2 3 4 | public IEnumerable<Product> GetAllProducts() { return repository.GetAll(); } |
1 2 3 4 5 6 7 8 9 | public Product GetProduct(int id) { Product item = repository.Get(id); if (item == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return item; } |
1 2 3 4 5 | public Product PostProduct(Product item) { item = repository.Add(item); return item; } |
1 2 3 4 5 6 7 8 | public void PutProduct(int id, Product product) { product.Id = id; if (!repository.Update(product)) { throw new HttpResponseException(HttpStatusCode.NotFound); } } |
1 2 3 4 5 6 7 8 9 10 | public void DeleteProduct(int id) { Product item = repository.Get(id); if (item == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } repository.Remove(id); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; using System.Web; using Microsoft.AspNet.SignalR; namespace SignalRChat { public class ChatHub : Hub { public void Send(string name, string message) { // Call the addNewMessageToPage method to update clients. Clients.All.addNewMessageToPage(name, message); } } } |
RouteTable.Routes.MapHubs();
1 2 3 4 | public ActionResult Chat() { return View(); } |
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 | @{ ViewBag.Title = "Chat"; } <h2>Chat</h2> <div class="container"> <input type="text" id="message" /> <input type="button" id="sendmessage" value="Send" /> <input type="hidden" id="displayname" /> <ul id="discussion"> </ul> </div> @section scripts { <!--Script references. --> <!--The jQuery library is required and is referenced by default in _Layout.cshtml. --> <!--Reference the SignalR library. --> <script src="~/Scripts/jquery.signalR-1.0.1.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="~/signalr/hubs"></script> <!--SignalR script to update the chat page and send messages.--> <script> $(function () { // Reference the auto-generated proxy for the hub. var chat = $.connection.chatHub; // Create a function that the hub can call back to display messages. chat.client.addNewMessageToPage = function (name, message) { // Add the message to the page. $('#discussion').append('<li><strong>' + htmlEncode(name) + '</strong>: ' + htmlEncode(message) + '</li>'); }; // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); }); // This optional function html-encodes messages for display in the page. function htmlEncode(value) { var encodedValue = $('<div />').text(value).html(); return encodedValue; } </script> } |
1 2 3 | bundles.Add(new ScriptBundle("~/bundles/signalr").Include( "~/Scripts/json2.js", "~/Scripts/jquery.signalR-{version}.js")); |
1 | @Scripts.Render("~/bundles/signalR") |