3

I need to get about 14 different input values from user to create a product and the input should not receive in the controller as null instead it should go as empty string

To achieve this the following is that I did: Controller:

var NewProductCatalog = {
    CustomerGuid: '00000000-0000-0000-0000-000000000000', SalesPartNo: "", Description: "",
    Name: "", Inventory: 0, UnitPrice: 0, BrandNo: "", TypeNo: "", LobNo: "", BrandName: "",
    TypeName: "", LobName: "", ManufacturePartNo: "", InventoryPartNo: "", EANCode: "",
    ImageURL: "", ProductType: "", DateAdded: "", Active: false, Tax: 0
};
$scope.newProductCatalog = NewProductCatalog;

.cshtml:

 <label for="SalesPartNo">Sales Part No *</label>
 <input type="text" required name="SalesPartNo" ng-model="newProductCatalog.SalesPartNo" class="form-control" />
 ...
 <input type="submit" ng-click="createProductCatalog(newProductCatalog)" />

Create function(controller):

$scope.createProductCatalog = function (NewProductCatalog) {
        var JsonString = angular.toJson(NewProductCatalog);
        CustomProductsServices.ProductCatalog_create(JsonString, function (callback) {
            if (callback.success) {
                $window.alert(callback.message);
            } else {
                $window.alert(callback.message);
            }
        });
    };

This allowed me to send an empty string when there user doesn't input any value.

The issue: When I try to enter a value in a text box and then delete/empty the text box and submit. I am not able to see the SalesPartNo in the resulting JSON string

Please point out the error of if my approach is wrong suggest me a better approach

2 Answers 2

2

Please remove attribute required from <input name="SalesPartNo" /> as angularjs not binding to model unless value is valid.

Please demo here http://jsbin.com/wuhipe/1/edit?html,console,output

<input type="text"  name="SalesPartNo" ng-model="newProductCatalog.SalesPartNo" class="form-control" />
Sign up to request clarification or add additional context in comments.

3 Comments

@RandomUser you can wrap your input into form and validate it before you send to server please see here jsbin.com/wuhipe/2/edit?console,output
@RandomUser please follow that example as you can see submit button not sending if form in not valid.
0

You can extend JsonString using the default one, say, angular has extend method. Make a copy of NewProductCatalog, say, NewProductCatalogCopy, and then do

angular.extend(NewProductCatalogCopy , JsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.