最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - ASP.NET WebForms with Angular Postback - Stack Overflow

programmeradmin5浏览0评论

im trying to use Angular framework in existing ASP.NET WebForms application and problem i ran into is this:

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}}"

How to get value of selected option on server side?

im trying to use Angular framework in existing ASP.NET WebForms application and problem i ran into is this:

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}}"

How to get value of selected option on server side?

Share Improve this question edited Oct 21, 2013 at 11:09 aron asked Oct 21, 2013 at 9:40 aronaron 1,9245 gold badges23 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can't use angular for server select tag. Actually, selected option value puted on server on postback. So as you have value="{{partner.Id}}" in markup, you getting exactly this value. In my opinion you can use plain select element without runat="server" attribute and bind selected id to hidden field, accessible on server-side:

<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). 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 dynamically generates content, which you don't have much control over. It would generate contains (like span), dynamic client ids an all to keep the data in sync and detect changes on the server.

I seriously doubt, if one can achieve data-binding in AngularJS for content generated with ASP.Net. The best that can work would be input type binding with ng-model and getting that data on the server with postback.

I highly remend you to switch to ASP.Net MVC

发布评论

评论列表(0)

  1. 暂无评论