在本文中,我们将给您介绍关于如何从MVC3中的javascript调用Controller方法?的详细内容,并且为您解答mvc调用webapi的相关问题,此外,我们还将为您提供关于(转)Model-V
在本文中,我们将给您介绍关于如何从MVC3中的javascript调用Controller方法?的详细内容,并且为您解答mvc调用webapi的相关问题,此外,我们还将为您提供关于(转)Model-View-Controller (MVC) with JavaScript、asp.net – 如何从javascript调用Web服务方法?、asp.net – 如何从mvc .net web app中的jquery(在自定义视图页面中)调用contoller post action、asp.net – 如何从MVC3中的控制器名称中删除“Controller”?的知识。
本文目录一览:- 如何从MVC3中的javascript调用Controller方法?(mvc调用webapi)
- (转)Model-View-Controller (MVC) with JavaScript
- asp.net – 如何从javascript调用Web服务方法?
- asp.net – 如何从mvc .net web app中的jquery(在自定义视图页面中)调用contoller post action
- asp.net – 如何从MVC3中的控制器名称中删除“Controller”?
如何从MVC3中的javascript调用Controller方法?(mvc调用webapi)
我正在使用MVC3架构,c#.net。当焦点更改到下一个字段(即密码字段)时,我需要立即将文本框内容(用户ID)与数据库进行比较。因此,我想对用户ID字段使用onblur事件,然后再调用Controller方法。有谁能告诉我如何解决这个问题?作为我的新手,代码片段受到高度赞赏。
提前致谢,
普拉山斯
答案1
小编典典这是一个例子。控制器方法示例
[HttpPost] // can be HttpGetpublic ActionResult Test(string id){ bool isValid = yourcheckmethod(); //.. check var obj = new { valid = isValid }; return Json(obj);}
这将是您的javascript函数。
function checkValidId(checkId){ $.ajax({ url: ''controllerName/Test'', type: ''POST'', contentType: ''application/json;'', data: JSON.stringify({ id: checkId }), success: function (valid) { if(valid) { //show that id is valid } else { //show that id is not valid } } });}
(转)Model-View-Controller (MVC) with JavaScript
原文:http://www.alexatnet.com/content/model-view-controller-mvc-javascript
The article describes an implementation of Model-View-Controller software design pattern in JavaScript.
I like JavaScript programming, because it is the most flexible language in the world. With the JavaScript language developers can create applications either in procedural or object-oriented style. It supports almost all programming styles and techniques that I know. I''ve seen procedural, object-oriented, aspect-oriented JavaScript code snippets. A determined developer can even use functional programming techniques in JavaScript applications.
My goal for this article is to write a simple JavaScript component that can show a power of the language. The component is a kind of the HTML ListBox ("select" HTML tag) control with an editable list of items: the user can select item and remove it or add new items into the list. Component will consist of three classes that implement the Model-View-Controller design pattern.
I hope, this article can be just a good reading for you, but it would be much better if you consider running the example and adapting it to you needs. I believe you have everything to create and run JavaScript programs: brains, hands, text editor (notepad), and Internet browser (IE or Firefox).
The Model-View-Controller pattern, which I''m about to use in the code, requires some description here. As you may know, the name of the pattern is based on the names of its main parts: Model, which stores an application data model; View, which renders Model for an appropriate representation; and Controller, which updates Model. Wikipedia defines typical components of the Model-View-Controller architecture as follows:
- Model: The domain-specific representation of the information on which the application operates. The model is another name for the domain layer. Domain logic adds meaning to raw data (e.g., calculating if today is the user''s birthday, or the totals, taxes and shipping charges for shopping cart items).
- View: Renders the model into a form suitable for interaction, typically a user interface element. MVC is often seen in web applications, where the view is the HTML page and the code which gathers dynamic data for the page.
- Controller: Processes and responds to events, typically user actions, and invokes changes on the model and perhaps the view.
The data of the component is a list of items, in which one particular item can be selected and deleted. So, the model of the component is very simple - it is stored in an array property and selected item property; and here it is:
/** * The Model. Model stores items and notifies * observers about changes. */ var ListModel = function (items) { this._items = items; this._selectedIndex = -1; this.itemAdded = new Event(this); this.itemRemoved = new Event(this); this.selectedIndexChanged = new Event(this); }; ListModel.prototype = { getItems : function () { return [].concat(this._items); }, addItem : function (item) { this._items.push(item); this.itemAdded.notify({item: item}); }, removeItemAt : function (index) { var item = this._items[index]; this._items.splice(index, 1); this.itemRemoved.notify({item: item}); if (index == this._selectedIndex) this.setSelectedIndex(-1); }, getSelectedIndex : function () { return this._selectedIndex; }, setSelectedIndex : function (index) { var previousIndex = this._selectedIndex; this._selectedIndex = index; this.selectedIndexChanged.notify({previous: previousIndex}); } };
Event is a simple class for implementing the Observer pattern:
var Event = function (sender) { this._sender = sender; this._listeners = []; }; Event.prototype = { attach : function (listener) { this._listeners.push(listener); }, notify : function (args) { for (var i = 0; i < this._listeners.length; i++) { this._listeners[i](this._sender, args); } } };
The View class requires defining controls for interacting with. There are numerous alternatives of interface for the task, but I prefer a most simple one. I want my items to be in a Listbox control and two buttons below it: "plus" button for adding items and "minus" for removing selected item. The support for selecting an item is provided by Listbox''s native functionality. A View class is tightly bound with a Controller class, which "... handles the input event from the user interface, often via a registered handler or callback" (from wikipedia.org).
Here are the View and Controller classes:
var ListView = function (model, controller, elements) { this._model = model; this._controller = controller; this._elements = elements; var _this = this; // attach model listeners this._model.itemAdded.attach(function () { _this.rebuildList(); }); this._model.itemRemoved.attach(function () { _this.rebuildList(); }); // attach listeners to HTML controls this._elements.list.change(function (e) { _this._controller.updateSelected(e); }); }; ListView.prototype = { show : function () { this.rebuildList(); var e = this._elements; var _this = this; e.addButton.click(function () { _this._controller.addItem() }); e.delButton.click(function () { _this._controller.delItem() }); }, rebuildList : function () { var list = this._elements.list; list.html(''''); var items = this._model.getItems(); for (var key in items) list.append($(''<option>'' + items[key] + ''</option>'')) this._model.setSelectedIndex(-1); } }; var ListController = function (model) { this._model = model; }; ListController.prototype = { addItem : function () { var item = prompt(''Add item:'', ''''); if (item) this._model.addItem(item); }, delItem : function () { var index = this._model.getSelectedIndex(); if (index != -1) this._model.removeItemAt(this._model.getSelectedIndex()); }, updateSelected : function (e) { this._model.setSelectedIndex(e.target.selectedIndex); } };
And of course, the Model, View, and Controller classes should be instantiated. The sample, which you can below, uses the following code to instantiate and configure the classes:
$(function () { var model = new ListModel([''PHP'', ''JavaScript'']); var view = new ListView(model, new ListController(model), { ''list'' : $(''#list''), ''addButton'' : $(''#plusBtn''), ''delButton'' : $(''#minusBtn'') } ); view.show(); }); view source print? <select id="list" size="10"></select><br/> <button id="plusBtn"> + </button> <button id="minusBtn"> - </button>
asp.net – 如何从javascript调用Web服务方法?
谢谢,
马特
解决方法
This topic explains how to use to call
a Web service from ECMAScript
(JavaScript). To enable your
application to call ASP.NET AJAX Web
services by using client script,the
server asynchronous communication
layer automatically generates
JavaScript proxy classes. A proxy
class is generated for each Web
service for which an
<asp:ServiceReference>
element is
included under the
<asp:ScriptManager>
control in the page.
asp.net – 如何从mvc .net web app中的jquery(在自定义视图页面中)调用contoller post action
我创建了一个名为SeatPlansController的mvc web app.contoller和名为SeatPlanes的模型,我可以将数据插入数据库.
但是我无法使用mvc控制器将数据从jquery传递到数据库.
我的控制器动作代码如下所示
// GET: SeatPlans/Create public ActionResult Create() { return View(); } // POST: SeatPlans/Create // To protect from overposting attacks,please enable the specific properties you want to bind to,for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(String seat_id,String seat_no) { int id = 10; SeatPlans S = new SeatPlans(); S.seat_id = seat_id; S.seat_no = seat_no; if (ModelState.IsValid) { db.SEATPLAN.Add(S); db.SaveChanges(); // return RedirectToAction("Index"); } return View(S); }
在帖子创建控制器Id是主键,所以我想传递seat_id,seat_no作为参数,它应该更新数据库.
我使用了以下javascript
function getPerson(id) { $.ajax({ type: "GET",url: '@Url.Action("create","SeatPlanesController")',contentType: "application/json; charset=utf-8",data: {eat_id :6,seat_no:8},dataType: "json",success: function (result) { alert(result); //window.locationre = result.url; } }); }
我可以使用运行create get方法
http://localhost:52348/SeatPlans/Create
但是如何直接从带有参数的浏览器运行post方法
就像是
http://localhost:52348/SeatPlans/Create/2/3t/e
我已将脚本更改为下方,它适用于GET方法,但如果我使用TYPE:“post”,则会弹出一个带警报的警告框
“localhost:52348说:
内部服务器错误”
$(document).ready(function () { $("button").click(function () { $.ajax({ type: "POST",url: '@Url.Action("Create","SeatPlans",new { Area = "" })',data: { seat_id: "34",seat_no: "98" },async: false,success: function (result) { $("#div1").html(result); },error: function (abc) { alert(abc.statusText); },}); }); });
解决方法
我改变了脚本如下
//jQuery.noConflict(); // var $j = jQuery.noConflict(); function clickevent() { var form = $("#frm"); var token = $('input[name="__RequestVerificationToken"]',form).val(); $.ajax({ type: "post",// headers: { "__RequestVerificationToken": token },"SeatPlans")',data: { seat_id: "34",seat_no: "98" },success: function (result) { $("#div1").html(result); } }); }
然后将创建帖子方法更改为以下方式
<pre> public ActionResult Create(String seat_id,String seat_no) { int id = 10; SeatPlans S = new SeatPlans(); S.seat_id = seat_id; S.seat_no = seat_no; if (ModelState.IsValid) { db.SEATPLAN.Add(S); db.SaveChanges(); // return RedirectToAction("Index"); } return View(S); }
asp.net – 如何从MVC3中的控制器名称中删除“Controller”?
现在该类已经在名为controllers的文件夹中.我不想在我的班级名字的末尾装饰.它并没有真正贡献任何东西.
我想知道如何改变这一点并更多地了解MVC3内部的内容.
解决方法
更长的答案是将自定义控制器工厂注入MVC管道以覆盖默认管理器工厂.可以在in this article找到该过程的完整描述,但基本上,您:
>实施IControllerFactory
做任何你想做的事.
>在Application_Start事件中,调用ControllerBuilder.Current.SetControllerFactory
以使用您自己的控制器替换默认控制器工厂.
MVC是一个高度可扩展的框架;虽然大多数组件的默认行为可能会满足您的需要,但您几乎总能用自己的内容替换内置对象.有关您可以覆盖的其他内容,请参阅13 extensibility points of MVC3的此列表.
另外,我最初忘了提到,但如果你真的对MVC的内心感兴趣,那么整个框架都是开源的(它在APL下),你可以找到它on codeplex.
今天关于如何从MVC3中的javascript调用Controller方法?和mvc调用webapi的分享就到这里,希望大家有所收获,若想了解更多关于(转)Model-View-Controller (MVC) with JavaScript、asp.net – 如何从javascript调用Web服务方法?、asp.net – 如何从mvc .net web app中的jquery(在自定义视图页面中)调用contoller post action、asp.net – 如何从MVC3中的控制器名称中删除“Controller”?等相关知识,可以在本站进行查询。
本文标签: