Asp.net MVC3,C#,找出单个或多个的Json放慢参数?(Asp.net MVC3, C#

2019-10-17 00:54发布

我传递给单个JSON类型参数和多JSON类型参数Asp.Net。

和在Asp.Net,我需要弄清楚该参数是单个数据或多种数据进行更新DB(EF,存储库模式)。

[单]

[多]

首先,我想这样的,

[HttpPost]
public ActionResult ItemUpdate(WEBORDERLN ln ,IList<WEBORDERLN> lns)
{

但它涉及到LN始终。 它从来没有去LNS :(

[HttpPost]
public ActionResult ItemUpdate(WEBORDERLN ln)
{
// it works for only single update

[HttpPost]
public ActionResult ItemUpdate(IList<WEBORDERLN> lns)
{
// it works for only multiple update

我怎么弄明白,JSON的类型参数是单个或多个?

任何人都知道,请咨询我。

谢谢!

[编辑]

[HttpPost]
public ActionResult ItemUpdate(WEBORDERLNS lns)
{
    try
    {
    EFWebOrderLnRepository webOrderLn = new EFWebOrderLnRepository();

    log.Debug("Count : "+lns.lns.Count.ToString()); // Object reference not set to an instance of an object.    
    log.Debug(lns.lns.Count());  //null, error : Value cannot be null
    log.Debug(lns.lns.Count().ToString());  //null, error : Value cannot be null

    foreach (WEBORDERLN ln in lns.lns)
    {
        webOrderLn.updateWebOrderLn(ln); // Error : Object reference not set to an instance of an object
    }
    .
    .
    .


public class WEBORDERLNS
{
    public IList<WEBORDERLN> lns { get; set; }
}

[编辑]

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Content-Length  875
Content-Type    application/json; charset=UTF-8
Cookie  user=38C6C07684B04645821CA5273EAB5B34340CE8CCA13A7D67DC748819B02C6963E4AD2A322068FC3090D3FE03EC81E663A5DED6FF2534E2595B40CF57EFF9BB544FFAC95CD79020E8A94019A3DC53B769EFC4049B6B9627E98AD9DBC1431BB5D99A47625B3353FA697A1CC005855C0248E95F61F7BD4A5362D75E5D01B395FB14E078524B8D0C7219AF959F9AED188AB30A16187DE166D5BD008B0013A65D470C52C9408C47FD42672B8FBABDB3524F77
Host    localhost:49995
Referer http://localhost:49995/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
X-Requested-With    XMLHttpRequest

[编辑]

Answer 1:

实际上,你会好得多创建模型类,它表示一个订单集,暴露的财产WEBORDERLN的集合。

public class OrderSet 
{
   public List<WEBORDERLN> OrderLines { get; set; }

}

在您的控制器的方法,传入的参数更改为: -

[HttpPost]
public ActionResult ItemUpdate(OrderSet orders)
{
// it works for only multiple update

在这一点上,在接收模式时,您可以只需拨打计数机制OrderLines财产。

// How many orders?
var orderCount = orders.OrderLines.Count;


文章来源: Asp.net MVC3, C#, Figure out Single or Multiple Json paramter?