2012-12-01 107 views
0

我得到了一个包含Person和Address的实体类。JSON将数据发布到mvc控制器

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

     public List<Address> Addresses { get; set; } 
} 

public class Address 
{ 
    public string ZipCode { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Country { get; set; } 
} 

在我看来,我显示了一些复选框。

@model DataContext.Models.Persons 

@{ 
    ViewBag.Title = "Person list"; 
} 

@using (Html.BeginForm("Update", "Test", FormMethod.Post)) 
{ 

    <div id="personContainer"> 
     @foreach(var t in Model.Person) 
     { 
      <input type="checkbox" value="@t.ID" name="@t.FirstName ">@t.FirstName <br /> 
     } 
    </div> 
    <p> 
     <input type="submit" id="save"> 
    </p> 
} 

我的控制器看起来是这样的:

[HttpPost] 
public JsonResult Update(Person p) 
{ 

    return Json(new { redirectTo = Url.Action("Index") }); 
} 

我要发布的数据必须是强类型。 如何将数据(在本例中为所有复选框)回传到使用JSON的'更新'控制器?

+0

是有什么可以帮助我解决这个问题吗? – Yustme

回答

2

您需要为您的视图模型的每个属性的输入。

每个输入的名称属性需要与MVC识别它的属性名称相同。

例如:

@model Person 

@using (Html.BeginForm("Update", "Test", FormMethod.Post)) 
{ 
    @Html.HiddenFor(model => model.ID) 

    <input type="checkbox" value="@model.FirstName" name="FirstName ">@model.FirstName <br /> 
    <input type="checkbox" value="@model.LastName" name="LastName ">@model.LastName <br /> 
    @Html.EditorFor(model => model.Addresses) 

    <input type="submit" id="save"> 
} 

对于地址,创建一个编辑模板,剃刀引擎将意识到,属性是可枚举。

的名称属性,这样它会增加枚举数:

<input id="addresses_0__city" type="text" value="City" name="addresses[0].city"> 

注意,名字有一个数组风格。

  • 要创建编辑器模板,请在名为“EditorTemplates”的“Shared”视图文件夹(或相关视图文件夹)中创建一个文件夹。
  • 创建一个名为Address的部分视图(与该类型相同)。
  • 添加要看到每个地址的HTML:

    @model Address 
    
    @Html.CheckBox("ZipCode", false, new { value = Model.ZipCode}) @Model.ZipCode 
    <br /> 
    @Html.CheckBox("City", false, new { value = Model.City}) @Model.City 
    <br /> 
    @Html.CheckBox("State", false, new { value = Model.State}) @Model.State 
    <br /> 
    @Html.CheckBox("Country", false, new { value = Model.Country}) @Model.Country 
    <br /> 
    

这是可行的,但它不会发布形式JSON。如果您需要专门的岗位作为JSON你可以在表单数据转换成JSON上的AJAX POST:

$.ajax("/Test/Update", 
{ 
    type: "post", 

    data: JSON.stringify($('form').serializeObject()), // converting to JSON 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function (data) {}  
    error: function() {} 
}); 

要JSON.stringify形式首次投入对象的形式使用这样的:

$.fn.serializeObject = function() { 
    var obj = {}; 
    var arr = this.serializeArray(); 

    $.each(arr, function() { 
     var splt = this.name.split(".", 2); 
     if (splt.length === 2) { 
      if (obj[splt[0]] === undefined) { 
       obj[splt[0]] = {}; 
      } 
      obj[splt[0]][splt[1]] = this.value || ''; 
     } 
     else if (obj[this.name] !== undefined) { 
      if (!obj[this.name].push) { 
       obj[this.name] = [obj[this.name]]; 
      } 
      obj[this.name].push(this.value || ''); 
     } 
     else { 
      obj[this.name] = this.value || ''; 
     } 
    }); 
    return obj; 
}; 
0

设置一个jQuery方法在表格上运行提交发送序列化形式的控制器在开始表格

$('formSelector').submit(function() { 
    if ($(this).valid()) { 
     $.ajax({ 
      url: this.action, 
      type: this.method, 
      data: $(this).serialize(), 
      success: function (result) { 
       if (result.Success == true) { 

       } 
       else { 
        $('#Error').html(result.Html); 
       } 
      } 
     }); 
    } 
    return false; 
}); 
+0

什么是formSelector?我试过了你的代码,但是当数据到达控制器时,数据是空的。我错过了什么吗? – Yustme

+0

@Yustme这将成为你的表格 的路径,所以如果你的表单的ID为“person”,你将有$('#person')。submit。如果这是页面上唯一的表单,您可以使用$('form')。submit来选择提交按钮。我假设你知道jQuery语法。 – Oli

+0

没有深厚的知识,只是一些基础知识。但是,你如何提交所有复选框与他们的ID以供日后参考? – Yustme

相关问题