【问题标题】:Knockout.js & list of checkboxes: post to mvc controllerKnockout.js 和复选框列表:发布到 mvc 控制器
【发布时间】:2012-05-28 17:38:06
【问题描述】:

我有一个看起来像这样的 MVC 视图模型:

public class DirectorySearchModel
{
    [Display(Name = "First name contains")]
    public string FirstName { get; set; }

    [Display(Name = "Last name contains")]
    public string LastName { get; set; }

    public CountriesCollection Countries { get; set; }
    public IEnumerable<Country> SelectedCountries { get; set; }
    public IEnumerable<Country> AllCountries { get; set; }
}

CountryCollection 对象(第 9 行)如下所示:

public class CountriesCollection
{
    [Display(Name = "Countries")]
    public int[] arrCountries { get; set; }
}

现在,我正在创建一个新的 CountryCollection 空白实例,然后将其添加到 DirectorySearchModel 视图模型的空白实例中,然后将其全部序列化为 Knockout.js 的 javascript 视图模型:

{
"FirstName":null,

"LastName":null,

"Countries":{"arrCountries":[]},

"SelectedCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}],

"AllCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}]
}

我的复选框呈现为:&lt;input checked="checked" data-bind="checked: Countries.arrCountries" id="Countries_arrCountries30" name="Countries.arrCountries" type="checkbox" value="1"&gt;。检查一对意味着你最终得到了这个 Knockout.js 视图模型:

{
"FirstName":null,

"LastName":null,

"Countries":{"arrCountries":["1", "3"]},

"SelectedCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}],

"AllCountries":[{"RelationshipManager":{},"CountryId":1,"CountryName":"UK"},{"RelationshipManager":{},"CountryId":2,"CountryName":"France"},{"RelationshipManager":{},"CountryId":3,"CountryName":"Spain"}]
}

正常提交我的视图(即通过提交按钮而不是使用 Knockout.js)到需要 DirectorySearchModel 的 MVC 操作,我可以要求 model.Countries.arrCountries 获取已检查项目的列表,但是当我使用...

$.post("/MyController/MyAction", ko.toJS(viewModel), function(returnData) {
    $("#resultCount").html(returnData);
});

或者...

$.post("/MyController/MyAction", viewModel, function(returnData) {
    $("#resultCount").html(returnData);
});

对于另一个期望相同DirectorySearchModel 的操作,model.Countries.arrCountries 始终是null!我想知道这是否是由于 Knockout.js 在 MVC 期待 int[]s 时将 arrCountries 条目发布为 string[]s,但是将我的 MVC 代码更改为期待 string[]s 似乎并没有太大变化......! DirectorySearchModel 中的 CountriesCollection 对象似乎存在,但它始终是 null 中的 arrCountries

有什么想法吗?非常感谢任何帮助!

编辑

接收 Knockout.js 视图模型的操作:

public MvcHtmlString ResultCount(DirectorySearchModel model)
{
    return new MvcHtmlString(getResultCount(model).ToString());
}

getResultCount 方法:

public int getResultCount(DirectorySearchModel model)
{
    IUserRepository userRepository = new UserRepository();
    int count = userRepository.Search(model, null).Count();
    return count;
}

已修复!

感谢 Konstantin 指出只需从 $.post 切换到 $.ajax 即可将我的 Knockout.js 视图模型发送回我的 mvc 操作!这是我正在使用的 $.ajax 代码:

$.ajax({  
    type: "POST",
    url: "/MyController/MyAction",
    data: ko.toJSON(viewModel),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        $("#resultCount").html(data);
    }
});

【问题讨论】:

  • 您可以使用 Fiddler 或内置的浏览器调试工具来查看网络上的实际情况吗?
  • 或者只是一个console.log(ko.toJS(viewModel)) 来确认发布的内容
  • 感谢各位的回复! Paul 的 console.log 建议显示,选中复选框后,您最终会得到:Countries (Object) 包含 arrCountries (Array[1]),其中包含 0: "2",所以它看起来确实像 Knockout.js viewModel 确实包含数据......似乎只是让我的 c# 接受它的一个案例!
  • @user1390429 您还在为此寻找答案吗?如果是这样,你能显示你的控制器方法吗
  • 嗨,保罗,我是。这是我的控制器:public MvcHtmlString ResultCount(DirectorySearchModel model) { return new MvcHtmlString(getResultCount(model).ToString()); },它调用:public int getResultCount(DirectorySearchModel model) { IUserRepository userRepository = new UserRepository(); int count = userRepository.Search(model, null).Count(); return count; }userRepository.Search 是一个标准的 query = query.Where(...) 交易,它根据输入/在模型中的信息添加子句。

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 knockout.js knockout-mapping-plugin


【解决方案1】:

您不能使用 $.post 您需要使用底层 $.ajax 并添加正确的内容类型以使 mvc 接受发布的 json 并进行模型绑定(内容类型应为“application/json; charset=utf-8 ") google 一下,你会看到很多例子

【讨论】:

  • 我知道这适用于 asp.net 表单,但我认为它不适用于 MVC
  • 感谢康斯坦丁!这就整理好了!几天的混乱,我的原始设置只需从 $.post 切换到 $.ajax 就可以完美运行!
  • 感谢 paul 和 Holf 的帮助 - 非常非常感谢您在这方面所做的所有工作!将使用我正在使用的 $.ajax 代码更新我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 2017-02-05
相关资源
最近更新 更多