本文介绍了$ HTTP POST与asp.net MVC模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么从有效载荷的angularjs $ HTTP POST不beeing绑定到输入模式?

当动作被称为型号为NULL和request.params和request.forms不显示形式的任何迹象beeing发送。但提琴手的请求表明,有效载荷beeing用J​​SON发送。

AngularJS:

  $ HTTP({
            方法:POST,
            网址:价格/加,
            数据:{
                ID:$ scope.id,
                STOREID:$ scope.storeid,
                名称:$ scope.name,
                制造商:$ scope.manufacturer,
                价格:$ scope.price
            }
        })

型号:

 公共类PriceModel
    {
        公众诠释?标识{搞定;组; }
        公众诠释? STOREID {搞定;组; }
        公共字符串酒吧code {搞定;组; }
        公共字符串名称{;组; }
        公共字符串制造商{搞定;组; }
        公众的DateTime创建{搞定;组; }
        公共双?价格{搞定;组; }
    }

控制器和操作方法的说明

 公共类PriceController:控制器
    {
        [HttpPost]
        公众诠释添加(PriceModel价格)
        {

菲德勒:

  POST HTTP://本地主机:4989 /价格/加HTTP / 1.1
主机:本地主机:4989
连接:保持活动
内容长度:70
接受:应用/ JSON,纯文本/ * / *
产地:HTTP://本地主机:4989
用户代理:Mozilla的/ 5.0(Windows NT的6.3; WOW64)为AppleWebKit / 537.36(KHTML,像壁虎)的Chrome / Safari浏览器33.0.1750.146 / 537.36
内容类型:应用程序/ JSON的;字符集= UTF-8
引用者:HTTP://本地主机:4989 /
接受编码:gzip,紧缩,SDCH
接受语言:NB,不,Q = 0.8,EN-US; Q = 0.6,连接; Q = 0.4{ID:,STOREID:,名:ASDF,制造商:ASDF,价:123}


解决方案

我不知道,如果模型绑定是困惑,因为该参数被命名为价格和你有一个在属性中的 PriceModel 这也被称为价格。你可以尝试重命名操作参数的名字吗?

Why is the payload from a angularjs $http post not beeing binded to the input model?

When the action is called the model is null and the request.params and request.forms does not show any sign of a form beeing sent. But the fiddler request shows that the payload is beeing sent with JSON.

AngularJS:

$http({
            method: "POST",
            url: "price/add",
            data: {
                Id: $scope.id,
                StoreId: $scope.storeid,
                Name: $scope.name,
                Manufacturer: $scope.manufacturer,
                Price: $scope.price
            }
        })

Model:

public class PriceModel
    {
        public int? Id { get; set; }
        public int? StoreId { get; set; }
        public string Barcode { get; set; }
        public string Name { get; set; }
        public string Manufacturer { get; set; }
        public DateTime Created { get; set; }
        public double? Price { get; set; }
    }

controller and action method description

public class PriceController : Controller
    {
        [HttpPost]
        public int Add(PriceModel price)
        {

Fiddler:

POST http://localhost:4989/price/add HTTP/1.1
Host: localhost:4989
Connection: keep-alive
Content-Length: 70
Accept: application/json, text/plain, */*
Origin: http://localhost:4989
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:4989/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: nb,no;q=0.8,en-US;q=0.6,en;q=0.4

{"id":"","storeid":"","name":"asdf","manufacturer":"asdf","price":123}
解决方案

I'm not sure if model binding is confused because the parameter is named price and you have a property in the PriceModel that's also called Price. Can you try renaming the action parameter name?

这篇关于$ HTTP POST与asp.net MVC模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 22:16