Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MVC3淘汰赛:淘汰赛模式获胜';t收集后数据?_Javascript_Asp.net Mvc_Json_Asp.net Mvc 3_Knockout.js - Fatal编程技术网

Javascript MVC3淘汰赛:淘汰赛模式获胜';t收集后数据?

Javascript MVC3淘汰赛:淘汰赛模式获胜';t收集后数据?,javascript,asp.net-mvc,json,asp.net-mvc-3,knockout.js,Javascript,Asp.net Mvc,Json,Asp.net Mvc 3,Knockout.js,回答: 替换此行: self.identifiers.push(new Identifier(self.identifierToAdd(), self.selectedIdentifierTypeValue())); self.identifiers.push({ Key: self.identifierToAdd(), Value: self.selectedIdentifierTypeValue()}); 这一行: self.identifiers.push(new Identifier

回答: 替换此行:

self.identifiers.push(new Identifier(self.identifierToAdd(), self.selectedIdentifierTypeValue()));
self.identifiers.push({ Key: self.identifierToAdd(), Value: self.selectedIdentifierTypeValue()});
这一行:

self.identifiers.push(new Identifier(self.identifierToAdd(), self.selectedIdentifierTypeValue()));
self.identifiers.push({ Key: self.identifierToAdd(), Value: self.selectedIdentifierTypeValue()});
post请求现在正在正确发送收集数据。然而,这并不能解决MVC行动没有收到它的事实,但这个问题已经足够大了


在发布到某个操作时,我似乎无法从我的模型的collection属性中获取数据。如果我从下面警告
ko.toJSON
my
identifiers()
属性,它会正确显示所有数据,但当我尝试通过正常回发提交该数据时(该操作仅采用下面的EquipmentCreateModel),它看起来是这样的:

identifiers:"[{\"Value\":\"sdfsd\",\"Key\":\"4554f477-5a58-4e81-a6b9-7fc24d081def\"}]"

标识符为空,当我查看ModelState错误中的标识符时,它表示它
无法将字符串转换为字典
。我做错了什么?我认为MVC3可以自动将JSON转换为对象,就像它在
BuildingCode
Room
属性中所做的一样

还有,为什么上图中的字符串数据有转义引号


编辑: 如果我查看post数据,标识符显示为空数组(
标识符:[{}]
)。我在save方法中尝试了jsoning标识符,如下所示:

self.identifiers = ko.toJSON(self.identifiers());
这会导致请求数据不为空,如下所示:

identifiers:"[{\"Value\":\"sdfsd\",\"Key\":\"4554f477-5a58-4e81-a6b9-7fc24d081def\"}]"
但是,在调试操作时也会出现同样的问题。我还尝试对整个模型进行JS控制(如中所述):

但是这会产生一个.NET错误,该错误表示由于对象的当前状态,
操作无效。
从查看请求数据来看,它看起来像是被JSON化了两次,因为每个字母或字符在HttpCollection中都是它自己的值,这是因为.NET默认情况下只允许1000 max()

使用$.ajax方法提交数据,一切正常:

 $.ajax({
            url: location.href, 
            type: "POST",
            data: ko.toJSON(viewModel),
            datatype: "json",
            contentType: "application/json charset=utf-8",
            success: function (data) { alert("success"); }, 
            error: function (data) { alert("error"); }
        });
但是由于其他原因,我不能使用$.ajax方法来实现这一点,所以我需要它在正常的帖子中工作。为什么我可以
toJSON
在ajax请求中使用整个viewModel,并且它可以工作,但是在正常的回发中它会将其拆分,而当我不这样做时,所有的引号都会在发送的JSON中转义


这是我的ViewModel:

public class EquipmentCreateModel
{
//used to populate form drop downs
public ICollection<Building> Buildings { get; set; }
public ICollection<IdentifierType> IdentifierTypes { get; set; }

[Required]
[Display(Name = "Building")]
public string BuildingCode { get; set; }

[Required]
public string Room { get; set; }

[Required]
[Range(1, 100, ErrorMessage = "You must add at least one identifier.")]
public int IdentifiersCount { get; set; } //used as a hidden field to validate the list
public string IdentifierValue { get; set; } //used only for knockout viewmodel binding

public IDictionary<Guid, string> Identifiers { get; set; }
}
<script type="text/javascript">
// Class to represent an identifier
function Identifier(value, identifierType) {
    var self = this;
    self.Value = ko.observable(value);
    self.Key = ko.observable(identifierType);
}

// Overall viewmodel for this screen, along with initial state
function AutoclaveCreateViewModel() {
    var self = this;

    //MVC properties
    self.BuildingCode = ko.observable();
    self.room = ko.observable("55");
    self.identifiers = ko.observableArray();
    self.identiferTypes = @Html.Raw(Json.Encode(Model.IdentifierTypes));
    self.identifiersCount = ko.observable();


    //ko-only properties
    self.selectedIdentifierTypeValue = ko.observable();
    self.identifierToAdd = ko.observable("");

    //functionality
    self.addIdentifier = function() {
        if ((self.identifierToAdd() != "") && (self.identifiers.indexOf(self.identifierToAdd()) < 0)) // Prevent blanks and duplicates
        {
            self.identifiers.push(new Identifier(self.identifierToAdd(), self.selectedIdentifierTypeValue()));
            alert(ko.toJSON(self.identifiers()));
        }
        self.identifierToAdd(""); // Clear the text box
    };

    self.removeIdentifier = function (identifier) {
        self.identifiers.remove(identifier);
        alert(JSON.stringify(self.identifiers()));
    };

    self.save = function(form) {
        self.identifiersCount = self.identifiers().length;
        ko.utils.postJson($("form")[0], self);
    };
}
    var viewModel = new EquipmentCreateViewModel();
    ko.applyBindings(viewModel);
    $.validator.unobtrusive.parse("#equipmentCreation");      
    $("#equipmentCreation").data("validator").settings.submitHandler = viewModel.save;
公共类设备CreateModel
{
//用于填充表单下拉列表
公共ICollection建筑物{get;set;}
公共ICollection标识符类型{get;set;}
[必需]
[显示(Name=“Building”)]
公共字符串构建代码{get;set;}
[必需]
公共字符串室{get;set;}
[必需]
[Range(1100,ErrorMessage=“您必须至少添加一个标识符。”)]
public int-IdentifiersCount{get;set;}//用作验证列表的隐藏字段
公共字符串IdentifierValue{get;set;}//仅用于剔除viewmodel绑定
公共IDictionary标识符{get;set;}
}
然后,我的淘汰脚本/视图模型:

public class EquipmentCreateModel
{
//used to populate form drop downs
public ICollection<Building> Buildings { get; set; }
public ICollection<IdentifierType> IdentifierTypes { get; set; }

[Required]
[Display(Name = "Building")]
public string BuildingCode { get; set; }

[Required]
public string Room { get; set; }

[Required]
[Range(1, 100, ErrorMessage = "You must add at least one identifier.")]
public int IdentifiersCount { get; set; } //used as a hidden field to validate the list
public string IdentifierValue { get; set; } //used only for knockout viewmodel binding

public IDictionary<Guid, string> Identifiers { get; set; }
}
<script type="text/javascript">
// Class to represent an identifier
function Identifier(value, identifierType) {
    var self = this;
    self.Value = ko.observable(value);
    self.Key = ko.observable(identifierType);
}

// Overall viewmodel for this screen, along with initial state
function AutoclaveCreateViewModel() {
    var self = this;

    //MVC properties
    self.BuildingCode = ko.observable();
    self.room = ko.observable("55");
    self.identifiers = ko.observableArray();
    self.identiferTypes = @Html.Raw(Json.Encode(Model.IdentifierTypes));
    self.identifiersCount = ko.observable();


    //ko-only properties
    self.selectedIdentifierTypeValue = ko.observable();
    self.identifierToAdd = ko.observable("");

    //functionality
    self.addIdentifier = function() {
        if ((self.identifierToAdd() != "") && (self.identifiers.indexOf(self.identifierToAdd()) < 0)) // Prevent blanks and duplicates
        {
            self.identifiers.push(new Identifier(self.identifierToAdd(), self.selectedIdentifierTypeValue()));
            alert(ko.toJSON(self.identifiers()));
        }
        self.identifierToAdd(""); // Clear the text box
    };

    self.removeIdentifier = function (identifier) {
        self.identifiers.remove(identifier);
        alert(JSON.stringify(self.identifiers()));
    };

    self.save = function(form) {
        self.identifiersCount = self.identifiers().length;
        ko.utils.postJson($("form")[0], self);
    };
}
    var viewModel = new EquipmentCreateViewModel();
    ko.applyBindings(viewModel);
    $.validator.unobtrusive.parse("#equipmentCreation");      
    $("#equipmentCreation").data("validator").settings.submitHandler = viewModel.save;

//类来表示标识符
函数标识符(值、标识符类型){
var self=这个;
自我价值=可观察到的价值;
self.Key=ko.observable(identifierType);
}
//此屏幕的整体视图模型以及初始状态
函数名为createViewModel(){
var self=这个;
//MVC属性
self.BuildingCode=ko.observable();
自我空间=可观察的高度(“55”);
self.identifiers=ko.observearray();
self.identiferTypes=@Html.Raw(Json.Encode(Model.identiferTypes));
self.identierscount=ko.observable();
//仅限ko酒店
self.selectedIdentifierTypeValue=ko.observable();
self.identifierToAdd=ko.可观察(“”);
//功能性
self.addIdentifier=函数(){
if((self.identifierLoadd()!=“”)和&(self.identifiers.indexOf(self.identifierLoadd())<0))//防止空白和重复
{
self.identifiers.push(新标识符(self.identifierLoadd(),self.selectedIdentifierTypeValue());
警报(ko.toJSON(self.identifiers());
}
self.IdentifierLoadd(“”;//清除文本框
};
self.removeIdentifier=函数(标识符){
self.identifiers.remove(标识符);
警报(JSON.stringify(self.identifiers());
};
self.save=函数(形式){
self.identifiersCount=self.identifiers().length;
ko.utils.postJson($(“form”)[0],self);
};
}
var viewModel=新设备createviewmodel();
应用绑定(视图模型);
$.validator.unobtrusive.parse(“equipmentCreation”);
$(“#设备创建”).data(“验证器”).settings.submitHandler=viewModel.save;

视图:

@使用(Html.BeginForm(“创建”,“设备”,FormMethod.Post,new{id=“设备创建”}))
{
@Html.ValidationSummary(true)
位置
@LabelFor(model=>model.BuildingCode)
@DropDownListFor(model=>model.BuildingCode,新建选择列表(model.Buildings,“BuildingCode”,“BuildingName”,“1091”),“--选择一个建筑--”,新建{data\u bind=“value:BuildingCode”})
@Html.ValidationMessageFor(model=>model.BuildingCode)
@LabelFor(model=>model.Room)
@Html.TextBoxFor(model=>model.Room,新的{@class=“inline width-7”,data_bind=“value:Room”})
@Html.ValidationMessageFor(model=>model.Room)
标识符
指定用于识别此高压灭菌器的任何独特属性

添加标识符 @DropDownList(“标识符下拉列表”,新建SelectList(Model.IdentifierTypes,“Id”,“Name”),新建{data\u bind=“value:selectedIdentifierTypeValue”}) @TextBox(“标识符值”,null,新的{@class=“inline width-15”,data_bind=“value:identifiertodd,valueUpdate:'afterkeydown'”) 添加 标识符类型 价值