【问题标题】:Sending data by using AngularJS to an action method of ASP.NET MVC 5 controller使用 AngularJS 将数据发送到 ASP.NET MVC 5 控制器的操作方法
【发布时间】:2015-11-03 21:46:38
【问题描述】:

我在我的项目中使用了Umbraco 7.3 和 ASP.NET MVC 5。

我想将数据从AngularJS 发送到ASP.NET MVC 5 控制器。

我该怎么做?

回复.html:

<div ng-controller="Reply.controller">
    <input type="button" name="Send Reply"  ng-click="SendReply()"/>
</div>

回复.controller.js:

angular.module("umbraco")
.controller("Reply.controller", function ($scope) {
    $scope.SendReply = function () {
        var SendTo = $("#Email").val();
        var TextMessage = $("#TextMessage").val();
        //TODO: It's need to write some codes to handle data to an action in ASP.NET MVC controller.But how?
    }
});

ASP.NET MVC 控制器:

public class IncomingCallSurfaceController : BaseSurfaceController
{
    public ActionResult Reply(SendMailModel sendMailModel)
    {
        //TODO: how I should be write this method that be proper for getting data from angularjs?
        return null;
    }
}

SendMailModel:

public class SendMailModel
{
    public string TextMessage { get; set; }
    public string SendTo { get; set; }
}

package.manifest:

{
propertyEditors: [
    {
        alias: "Send.Reply",
        name: "Send Reply",
        editor:{
            view:"/App_Plugins/Reply/Reply.html"
        },
    }
]
,
javascript:[
    '/App_Plugins/Reply/Reply.controller.js'
]
}

更新:添加解决方案文件夹结构图片

【问题讨论】:

    标签: ajax angularjs asp.net-mvc umbraco umbraco7


    【解决方案1】:

    回复.controller.js:

    angular.module("umbraco")
    .controller("Reply.controller", function ($scope, $http, $routeParams) {
        $scope.SendReply = function () {
            var sendTo = $("#Email").val();
            var textMessage = $("#TextMessage").val();
            var contentId = $routeParams.id;
            $scope.xxx = "I'm here!";
    
            var dataObj = {
                TextMessage: textMessage,
                SendTo: sendTo,
                ContentId: contentId
            };
            $http.post("backoffice/Reply/ReplyToIncomingCall/ReplyMessage", dataObj)
               .then(function (response) {
                   alert("YES!");
                   //TODO: 
               });
        }
    });
    

    ReplyToIncomingCallController.cs:

    namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
    {
       [PluginController("Reply")]
       public class ReplyToIncomingCallController :UmbracoAuthorizedJsonController
       {
           [HttpPost][ChildActionOnly]
           public ActionResult ReplyMessage(SendMailViewModel vm)
           {
    
               return null;
           }
       }
    }
    

    SendMailViewModel:

     public class SendMailViewModel
     {
        public string TextMessage { get; set; }
        public string SendTo { get; set; }
        public int ContentId { get; set; }
     }
    

    文件的树形结构:

    如果您想了解更多关于 Umbraco 7.x 中后台路由的信息,可以访问this link

    【讨论】:

      【解决方案2】:

      首先,当你使用 Angular 时,尽量避免使用 jQuery。

      使用 ng-model from angular 将输入值绑定到控制器。并使用 $http 模块向 API 发送数据。

      查看:

      <div ng-controller="Reply.controller">
          <input type="text" ng-model="message.TextMessage"/>
          <input type="text" ng-model="message.SendTo"/>
      
          <input type="button" name="Send Reply"  ng-click="SendReply()"/>
      </div>
      

      角度:

      angular.module("umbraco")
          .controller("Reply.controller", function($scope, $http) {
      
              $scope.message = {
                  TextMessage: '',
                  SendTo: ''
              };
      
              $scope.SendReply = function() {
                  //TODO URL 
                  $http.post('/umbraco/api/IncomingCallSurface/Reply', $scope.message)
                      .then(function(response) {
      
                          //TODO 
      
                      });
      
              }
          });
      

      ASP.NET MVC:

      public class IncomingCallSurfaceController : UmbracoApiController
      {
          [HttpPost]
          public ActionResult Reply(SendMailModel sendMailModel)
          {
              //TODO: how I should be write this method that be proper for getting data from angularjs?
              return null;
          }
      }
      

      【讨论】:

      • 在下面的评论中,我根据您的指南编写了我的代码,但我在 Umbraco 后台收到错误消息:请求错误:URL 返回 404(未找到):/IncomingCallSurface/Reply
      • var data = { SendTo: sendTo, TextMessage: textMessage }; $http.post("/IncomingCallSurface/Reply", data) .then(function (response) { //TODO });
      • 可以,但是需要设置URL的改进。我不确定,但是有了这份文档,您有两个选择 - our.umbraco.org/documentation/reference/routing/…:1. /umbraco/surface/IncomingCallSurface/Reply 2. /umbraco/Reply/IncomingCallSurface/Reply
      • 请看我添加的图片。根据你的建议,我必须改变结构吗?这意味着添加视图文件夹嵌套回复文件夹并将控制器添加到回复文件夹而不是控制器文件夹?
      • 您需要将其分解为简单的步骤。如果您之前从未编写过 UmbracoApiController,请这样做。开始简单。使用 HttpGet 而不是 HttpPost 创建测试方法。让它返回一些测试字符串(“Hello Word”)。直接在浏览器中调用它。当你得到那个工作 + 文本显示在你的浏览器中然后编写你的 Angular 代码以从同一个 URL 检索。使用 Fiddler 等工具检查您的 URL 调用是否正确。 TLDR:确保您从控制器对 URL 提供正确的操作,并且这些 URL 由您的 Angular 调用。如果你不这样做,你会得到 404。
      猜你喜欢
      • 2012-02-22
      • 2015-02-09
      • 2016-03-28
      • 2021-12-28
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 2019-01-26
      • 2015-08-08
      相关资源
      最近更新 更多