2014-11-21 124 views
0

我是新的漂亮的烬宝js开发。如何将价值的余烬视图传递给控制器​​

我已经做视图下方代码

{{view "select" content=model prompt="Please select a name" selectionBinding="" optionValuePath="content.body" optionLabelPath="content.title"}} 

使用以下的Json

posts = [{ 
    title: "Raja", 
    body: "There are lots of à la carte software environments in this world." 
}, { 
    title: "Broken Promises", 
    body: "James Coglan wrote a lengthy article about Promises in node.js." 
}]; 

和路由器

App.InRoute = Ember.Route.extend({ 
    model: function() {  

     return posts;  
    } 
}); 

我的要求是使该组合框选择的值提供给控制器

App.InController = Ember.Controller.extend({ 

alert("combobox selected item") 
}); 

怎么的我访问值apicontoller在.NET MVC 4

public class ValuesController : ApiController 
    { 
    string value= combo box selected value 
} 

回答

0

你“中选择”视图的属性需要绑定到控制器上的一个属性:

以下内容添加到您的视图的属性:值=将selectedItem

在你的控制器:

添加 “将selectedItem

App.InRoute = Ember.Route.extend({ 

    selectedItem: null, 

    model: function() { 
     return posts;  
    } 
}); 

现在你的所有设置发送它到你的Api端点。你可以创建一个动作处理器,并在那里进行。下面是一个简单的例子:

App.InRoute = Ember.Route.extend({ 

    selectedItem: null, 

    model: function() { 
    return posts;  
    }, 

    actions: { 
    submit: function(){ 
     $.ajax('/api/yourEndPoint', {type: 'POST', data: {body: this.get('selectedItem')} }) 
    } 
} 
}); 

在你的手把模板

<button {[action 'submit'}}>Submit</button>

在你的.NET API控制器

public IHTTPActionResult Post(string body){ 
    //.NET's Model Binder will correctly pull out the value of the body keyvalue pair. 
    //Now do with "body" as you will. 
} 

你真的应该看看使用E mber-Data,这真是太棒了。

+0

我试图显示警报动作:{ 提交:函数(){ 警报(this.get( '将selectedItem')); }显示空值 – raj 2014-11-24 07:01:09

+0

正如Asgaroth指出的那样,您还可以绑定到您的模型上的某个属性或控制器,或者可以工作。如果您使用值,请不要使用selectionBinding。 http://emberjs.com/api/classes/Ember.Select.html – 2014-11-25 14:49:47

0

你只需要设置selectionBinding="someModelAttribute"和双向数据绑定将在设置的选定值的护理该模型。

+0

ü可以简单介绍一下 – raj 2014-11-24 07:03:30

相关问题