Sending data by using AngularJS to an action method of ASP.NET MVC 5 controller

老子叫甜甜 提交于 2019-12-01 06:40:40

问题


I've used Umbraco 7.3 and ASP.NET MVC 5 in my project.

I want to Send data from AngularJS to ASP.NET MVC 5 controller.

How can I do it?

reply.html :

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

Reply.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 controller:

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'
]
}

Updated: add a picture of structure of solution folders


回答1:


Reply.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; }
 }

Tree Structure Of Files :

If you want to know more about backoffice-routing in Umbraco 7.x, you can visit this link.




回答2:


Firstly, when you use Angular try to avoid use jQuery.

Use ng-model from angular to bind input value to your controller. And use $http module to send data to API.

View:

<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:

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


来源:https://stackoverflow.com/questions/33509696/sending-data-by-using-angularjs-to-an-action-method-of-asp-net-mvc-5-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!