Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have a problem code. like this

View

 <div ng-app="MyPct4" ng-controller="ajaxCtrl">
    <div>
        <table>
            <tr>
                <td><b>pls input Color</b></td>
                <td><input type="text" ng-model="InputColor" /></td>
            </tr>
        </table>
        <input type="button" value="save" ng-click="AddUpdateColor()" />
    </div>
</div>

Angularjs controller

var ptc4 = angular.module("MyPct4", []);
ptc4.controller('ajaxCtrl', function ($scope, myService) {
    $scope.AddUpdateColor = function () {
                var newColor = { Color: $scope.InputColor };
                var getData = myService.AddColor(newColor);
                getData.then(function (msg) {
                    alert(msg.data);
                }, function () {
                    alert("error")
                });
            }
}

Angularjs service

ptc4.service("myService", function ($http) {
this.AddColor = function (newColor) {
                var response = $http({
                    method: "post",
                    url: "/Practice/AddColor",
                    data: JSON.stringify(newColor),
                    dataType: "json"
                });
                return response;
            }
}

MVC controller

 private TestDBEntities2 db = new TestDBEntities2();
    public string AddColor(ColorDB color)
        {
            if (color != null)
            {
                    db.ColorDB.Add(color);
                    db.SaveChanges();
                    return "add success";
            }
            else
            {
                return "add fail";
            }
        }

The result is always show alert add fail. Seems to be json can't post to MVC controller. Please help thank you so much.

ColorDB

 public partial class ColorDB
{
    public int Id { get; set; }
    public string Color { get; set; }
}

update: I changed AddColor controller code.I also tyr to add ColorDB color = new ColorDB(); and color.Color = "testColor";The value can insert to db but ColorDB colalso null.The problem seems to Mvc Controller can't Receive ajax data.

public string AddColor(ColorDB col)
        {
            ColorDB color = new ColorDB();
            color.Color = "testColor";
            if (color != null)
                {
                    db.ColorDB.Add(color);
                    db.SaveChanges();
                    return "add success";
                }
                else
                {
                    return "add fail";
                }
            }
share|improve this question
    
Have you even try to debug your code? Your color argument is probably null, since you are sending a string and expecting a complex object. – jpgrassi Jul 22 at 11:16

public string AddColor(ColorDB color) you are expecting ColorDB object. If it represents your DB then its wrong. It could not bind. Check the parametres type .

UPDATE: I think the problem is binding.The controller can't bind your json to ColourDB object.

You can try:

Add this your ajax options contentType: "application/json; charset=utf-8". Maybe dataType: "json" is not enough. And if it wont work try to use angular.TOJSON(newColor) instead of using JSON.stringify(newColor). Or combine this two.

share|improve this answer
    
hi kyur my ColorDB like that.As in the preceding – zhan Jul 22 at 11:57
    
I tried, but I can't Thanks for your advice – zhan Jul 22 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.