简体   繁体   English

带有角度回发的ASP.NET WebForms

[英]ASP.NET WebForms with Angular Postback

im trying to use Angular framework in existing ASP.NET WebForms application and problem i ran into is this: 我试图在现有的ASP.NET WebForms应用程序中使用Angular框架,遇到的问题是:

Client side code: 客户端代码:

<select id="fooSelect" class="span8" runat="server" clientidmode="Static">
 <option ng-repeat="foo in foos" value="{{foo.Id}}">{{foo.Title}}</option>
</select>

When form gets submited (whole page postback), value of fooSelect.Value is "{{foo.Id}}" fooSelect.Value表单时(整个页面回发), fooSelect.Value值为"{{foo.Id}}"

How to get value of selected option on server side? 如何在服务器端获取所选选项的值?

You can't use angular for server select tag. 您不能将angular用于服务器选择标记。 Actually, selected option value computed on server on postback. 实际上,在回发时在服务器上计算所选的选项值。 So as you have value="{{partner.Id}}" in markup, you getting exactly this value. 因此,当你在标记中有value="{{partner.Id}}"时,你就得到了这个值。 In my opinion you can use plain select element without runat="server" attribute and bind selected id to hidden field, accessible on server-side: 在我看来,你可以使用没有runat="server"属性的普通select元素,并将选定的id绑定到隐藏字段,可在服务器端访问:

<select ng-model="partner" ng-options="p.Title for p in partners" >
    <option value="">--Select partner--</option>
</select>
<br />
<input type="hidden" id="selectedPartnerId" runat="server" ng-value="partner.Id" />

ASP.Net is not a good candidate for integration with AngularJS (and maybe other SPA). ASP.Net不是与AngularJS(以及其他SPA)集成的理想选择。 The abstraction against which ASP.Net is build makes it nearly impossible to leverage any capability of Angular such as routing or data binding. 构建ASP.Net的抽象使得几乎不可能利用Angular的任何功能,例如路由或数据绑定。

ASP.Net dynamically generates content, which you don't have much control over. ASP.Net动态生成内容,您对此没有太多控制权。 It would generate contains (like span), dynamic client ids an all to keep the data in sync and detect changes on the server. 它将生成包含(如span),动态客户端ID以保持数据同步并检测服务器上的更改。

I seriously doubt, if one can achieve data-binding in AngularJS for content generated with ASP.Net. 我非常怀疑,如果可以在AngularJS中为ASP.Net生成的内容实现数据绑定。 The best that can work would be input type binding with ng-model and getting that data on the server with postback. 可行的最佳方法是使用ng-model进行input类型绑定,并使用回发在服务器上获取该数据。

I highly recommend you to switch to ASP.Net MVC 我强烈建议您切换到ASP.Net MVC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM