2014-11-21 123 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

相關問題