相关疑难解决方法(0)

如何使用JSON,jQuery向ASP.NET MVC Controller发布复杂对象数组?

我当前的代码如下所示.如何将我的数组传递给控制器​​以及控制器操作必须接受哪种参数?

function getplaceholders() {
    var placeholders = $('.ui-sortable');
    var result = new Array();
    placeholders.each(function() {
        var ph = $(this).attr('id');
        var sections = $(this).find('.sort');
        var section;

        sections.each(function(i, item) {
            var sid = $(item).attr('id');

            result.push({ 'SectionId': sid, 'Placeholder': ph, 'Position': i });
        });
    });
    alert(result.toString());
    $.post(
        '/portal/Designer.mvc/SaveOrUpdate',
        result,
        function(data) {
            alert(data.Result);
        }, "json");
};
Run Code Online (Sandbox Code Playgroud)

我的控制器动作方法看起来像

public JsonResult SaveOrUpdate(IList<PageDesignWidget> widgets)
Run Code Online (Sandbox Code Playgroud)

javascript c# asp.net-mvc jquery json

92
推荐指数
5
解决办法
12万
查看次数

将数组作为JSON发布到MVC控制器

我一直在努力寻找这个问题的解决方案.

在我的代码中,我正在构建一个对象数组;

var $marks = [];

var mark = function ( x, y, counter ){
    this.x = x;
    this.y = y;
    this.counter = counter;
}

$marks.push(new mark(1, 2, 0));   
$marks.push(new mark(1, 2, 1));
$marks.push(new mark(1, 2, 2));
Run Code Online (Sandbox Code Playgroud)

现在我想将这些数据发布到MVC控制器,所以我认为控制器中的数据类型将是List<Mark> MarksMarks的数组或数组.

为了发布数据,我试过了;

var json = JSON.stringify($marks);
$.post('url', json).done(function(data){ /* handle */ });
Run Code Online (Sandbox Code Playgroud)

要么

var json = { Marks: $marks };
$.post('url', json).done(function(data){ /* handle */ });
Run Code Online (Sandbox Code Playgroud)

第二种方式,在查看发布的数据时,看起来像这样

Marks[0][x]: 1
Marks[0][y]: 2
Marks[0][counter]: 0
Marks[0][x]: 1
Marks[0][y]: 2
Marks[0][counter]: 1
Marks[0][x]: …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc json asp.net-mvc-4

16
推荐指数
2
解决办法
2万
查看次数

Asp.Net mvc,张贴json?

我的iphone客户端将以下json发布到我的mvc服务.从html表单发布数据时,它会自动将表单数据转换为UserModel并将对象传递给我的Create方法,但是当我从iphone发送请求正文中的JSON字符串时,它返回null.

从JSON到Object的转换最干净的解决方案是什么.

我宁愿不为不同的客户端创建多个方法,所以我试图在iphone和mvc客户端上使用相同的方法.

我的要求的身体:

{
   "firstName" : "Some Name",
   "lastName" : "Some Last Name",
   "age" : "age"
}
Run Code Online (Sandbox Code Playgroud)

我的模型和行动结果

public class UserModel
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Age { get; set; }
}

[HttpPost]
public Create ActionResult(UserModel user)
{
   // user is null
   userStorage.create(user);
   return SuccessResultForModel(user);
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc

12
推荐指数
2
解决办法
4万
查看次数

通过JSON将数组发送到MVC控制器?

我正在努力,通过JSON将数组发送到MVC控制器动作.

这是我拥有的和我尝试过的......

//Get checked records
var $checkedRecords = $(':checked'); //e.g. 3 rows selected = [input 4, input 5, input 6]

//Have tried following:
sendingVar: $checkedRecords.serializeArray(); // gives array of 0's
sendingVar: JSON.stringify($checkedRecords); // gives "{\"length\":1,\"prevObject\":{\"0\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"context\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"length\":1},\"context\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"selector\":\":checked\",\"0\":{}}"...wtf

//Post
$.post(url, { sendingVar: sendingVar }, function(data) {alert(data); });
Run Code Online (Sandbox Code Playgroud)

我该怎么做 ?

编辑:建议那些建议$checkedRecords从顶线发送"按现状" - 这是行不通的.我在jquery框架中的某个地方得到一个奇怪的异常:(

uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://.../.../.../jquery-1.4.4.min.js :: <TOP_LEVEL> :: line 141" data: no]
Run Code Online (Sandbox Code Playgroud)

我认为这意味着它试图将null分配给它不能的东西.

编辑:我使用MVC2而不是3

编辑2:在@ Monday的答案之后 - 问题是由于我是如何构建数组 …

c# asp.net-mvc jquery json

8
推荐指数
1
解决办法
2万
查看次数

DefaultModelBinder无法反序列化作为JSON对象传递给操作的.NET Dictionary对象?

我有一个非常简单的课程:

public class FilterItem
{
    public Dictionary<string, string> ItemsDictionary { get; set; }

    public FilterItem()
    {
        ItemsDictionary = new Dictionary<string, string>();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在客户端上填充字典中的数据,然后将其作为JSON对象传递给我的控制器操作.但无论我在客户端上尝试什么,DefaultModelBinder似乎都无法反序列化它.

以下是调用我的操作的示例javascript代码:

var simpleDictionary = {"ItemsDictionary": {"1": "5", "2": "7"}};

$.ajax({ cache: false, type: "POST", data: JSON.stringify(simpleDictionary),
contentType: "application/json; charset=utf-8", 
url: "/Catalog7Spikes/GetFilteredProductsJson", success: function (data) {...});
Run Code Online (Sandbox Code Playgroud)

这是我的动作方法的简化版本:

[HttpPost]
public ActionResult GetFilteredProductsJson(FilterItem filterItem)
{   
    ProductsModel productsModel = new ProductsModel();
    return View("SevenSpikes.Nop.UI.Views.Products", productsModel);
}
Run Code Online (Sandbox Code Playgroud)

请注意,相反的工作.当作为JsonResult传递时,FilterItem对象被成功序列化并作为JSON对象传递给客户端.然而,尝试反过来却行不通.

我读了关于Connect的票,并认为解决方法可行,但事实并非如此.

是否有可能使用ASP.NET MVC 3中的DefaultModelBinder反序列化.NET字典?

c# json defaultmodelbinder deserialization asp.net-mvc-3

6
推荐指数
1
解决办法
3153
查看次数

Asp.net Mvc Ajax Json(后阵列)

JavaScript的\ jQuery的:

 var items = new Array();

 var obj { Begin: "444", End: "end" };

 items.push(obj);
 items.push(obj);

  var request = {
             DateStart: $("#DateStart").val(),
             mass: items
         };


 $.post("/Home/Index", request, null,
 "json");
Run Code Online (Sandbox Code Playgroud)

C#Mvc索引控制器

 public class MyClass
     {
        public string Begin;
        public string End;
     }

     [AcceptVerbs(HttpVerbs.Post)]        
     public ActionResult Index(            
         string DateStart,            
         MyClass []mass)
     {
         System.Diagnostics.Debug.WriteLine(mass[0].Begin);
     }
Run Code Online (Sandbox Code Playgroud)

如何执行此代码?谢谢.

asp.net ajax asp.net-mvc json

5
推荐指数
0
解决办法
2万
查看次数

动态形式建筑与knockoutjs

我需要从数据库构建一个动态表单.我有跟随实体来动态定义表单字段:

    public class FormField {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }    // Possible values are: 'Radio','Combo','Text'. A dropdown will be created for a Combo type of element, a radio set for Radio type of element and a text input for Text type of element.
        public string Options { get; set; } // Only relevant in case of Radio/Combo type
        public string Default { get; set; …
Run Code Online (Sandbox Code Playgroud)

ajax form-post json.net asp.net-mvc-3 knockout.js

5
推荐指数
1
解决办法
6002
查看次数

在Ajax.BeginForm中发送javascript数组

我正在研究项目,并且我需要发送javaScript数组“ selectedZonesList”,将数据与表单数据一起返回给控制器。有人给我建议使用Ajax.BeginForm ...但是我正在努力地将所有部分集结起来,非常感谢...

在局部视图

@using (Html.BeginForm("CreateNewFeeScheme", "Qualification", FormMethod.Post, new { id = "NewFeeSchemeForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

   //rest of code to take user input for all variables ..

<input type="submit" value="Create" class="btn btn-default" />
}
Run Code Online (Sandbox Code Playgroud)

JavaScript功能

 <script type="text/javascript">

 var selectedZonesList = new Array();

 function AddFeeZoneToScheme(e)
 {

    var entityGrid = $("#FeeZoneGrid_02").data("kendoGrid");

    var selectedZone = entityGrid.dataItem(entityGrid.select());

    selectedZone = selectedZone.FeeZoneID;

    selectedZonesList.push(selectedZone); 
}

</script>
Run Code Online (Sandbox Code Playgroud)

控制者

  [HttpPost]
    public ActionResult CreateNewFeeScheme(FeeScheme newSchemeData, ??????)
    {
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc jquery ajax.beginform razor

5
推荐指数
1
解决办法
3443
查看次数

使用JSON,JQuery向ASP.NET MVC Controller发布一系列复杂对象

我知道这个问题已经提到了如之前在这里

但解决方案似乎不适合我的问题.

这是我的HTML.行数是可变的

 <table id="workPlanTable">
    <tr>
        <th>
            Begin
        </th>
        <th>
            End
        </th>
    </tr>

    <tr itemId="1">
        <td><input class="begin" id="begin_1" name="begin_1" type="text" value="5:30" /></td>
        <td><input class="end" id="end_1" name="end_1" type="text" value="11:30" /></td>
    </tr>
    <tr itemId="3">
        <td><input class="begin" id="begin_3" name="begin_3" type="text" value="5:30" /></td>
        <td><input class="end" id="end_3" name="end_3" type="text" value="7:30" /></td>
    </tr>

</table>
Run Code Online (Sandbox Code Playgroud)

js构建一个对象数组并将它们发布到控制方法中

<script type="text/javascript">
$(function() {

    submitForm = function() {
        var items = new Array();
        $("#workPlanTable tr").each(function(i) {

            var end = $(this).find(".end").val();
            var begin = $(this).find(".begin").val();

            var item = {
                "Begin": begin, …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc jquery json

4
推荐指数
1
解决办法
9727
查看次数

MVC3 RC2 JSON Post绑定无法正常工作

我已经看过关于这个主题的其他帖子,并且已经摆弄了变化,但仍然无法让JSON模型绑定正常工作.

我在global.asax.cs Application_Start方法中有以下内容:

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
Run Code Online (Sandbox Code Playgroud)

回发后的数据如下所示:

{"UserName":"Mike","Password":"password","Persist":true}
Run Code Online (Sandbox Code Playgroud)

我的PoCo:

public class UserLoginViewModel {
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool Persist { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器方法正确触发,但具有UserName = null,Password = null和Persist = false的默认UserLoginViewModel对象; 签名看起来像这样:

[HttpPost]
public ActionResult Logon(UserLoginViewModel model) {
    if (ModelState.IsValid) { 
    ...
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc json model-binding

4
推荐指数
1
解决办法
2228
查看次数

通过http GET将JSON对象发送到服务器

我正在寻找通过GET将JSON对象发送到服务器.Chris 通过JSON将对象数组发布到ASP.Net MVC3的答案适用于http POST,但不适用于GET.我的情况也适用于POST但不适用于GET.我可以做些什么让GET工作这是我的情况:在Controller中我有以下方法public ActionResult Screenreport(Screentable screendata)

   {
       // do something here
       return View();
   }
Run Code Online (Sandbox Code Playgroud)

我有两个ModelView如下:

   public class Screenrecord
   {
      public string Firstname{ get; set; }
      public string Lastname{ get; set; }
   }
   public class Screentable
   {
      public List<Screenrecord> Screenlist { get; set; } 
   }
Run Code Online (Sandbox Code Playgroud)

在客户端,我生成JSON对象

var Screentable = { Screenlist: screendata };
Run Code Online (Sandbox Code Playgroud)

screendata是一个Screenrecord数组

所有这些工作当我使用POST,但当我使用GET时,我得到空值(screendata = null)控制器的方法.换句话说,当单击GO时,屏幕截图(Screentable screendata)例程中的screendata为null.

此外,如果我发送一个JSON对象它可以工作,但如果我发送像我描述的数组(列表),它不会.我想做的是可行的吗?

json asp.net-mvc-3

3
推荐指数
1
解决办法
1万
查看次数