knockoutjs映射选择数据绑定选项(knockoutjs mapping select data-bind options)
我有问题将选择框的选定值绑定到视图模型中的属性。 出于某种原因,在回发到服务器时,它会保持不变。
我的Html是:
<form action="/Task/Create" data-bind="submit: save"> <table border="1"> <tr> <td>ref type</td> <td><select data-bind="options: ReferenceTypes, optionsText: 'Name', optionsCaption: 'Select...', value:Task.ReferenceTypeId"></select></td> <td>Reference</td> <td><input data-bind="value:Task.Reference" /></td> </tr> </table> <button type="submit">Save Listings</button> </form>
Javascript是:
<script type="text/javascript"> var viewModel = {}; $.getJSON('/Task/CreateJson', function (result) { viewModel = ko.mapping.fromJS(result.Data); viewModel.save = function () { var data = ko.toJSON(this); $.ajax({ url: '/Task/Create', contentType: 'application/json', type: "POST", data: data, dataType: 'json', success: function (result) { ko.mapping.updateFromJS(viewModel, result); } }); } ko.applyBindings(viewModel); }); </script>
来自Fiddler的JSON将被加载到页面中,如下所示。
{ "ContentEncoding":null, "ContentType":null, "Data":{ "Task":{ "ReferenceTypeId":0, "Reference":"Default Value" }, "ReferenceTypes":[ { "Id":2, "Name":"A Ref Type" }, { "Id":3, "Name":"B Ref Type" }, { "Id":1, "Name":"C Ref Type" } ] }, "JsonRequestBehavior":1 }
这将以更新的引用字符串值正确返回到服务器(ASP.NET MVC3)中,但ReferenceTypeId未绑定到正确选择的下拉值。 我是否需要执行任何附加功能来正确绑定等? 或者告诉数据绑定什么是选择值列(Id)等? 我已经在Fiddler中检查了从浏览器返回的值,并且它具有相同的原始值(0)。 所以它肯定不是服务器。
我希望有人可以提供帮助,如果您需要更多信息,请询问。
亲切的问候菲尔
I am having problems binding the selected value of a selectbox to a property within the view model. For some reason it keeps coming back unchanged when posted back to the server.
My Html is:
<form action="/Task/Create" data-bind="submit: save"> <table border="1"> <tr> <td>ref type</td> <td><select data-bind="options: ReferenceTypes, optionsText: 'Name', optionsCaption: 'Select...', value:Task.ReferenceTypeId"></select></td> <td>Reference</td> <td><input data-bind="value:Task.Reference" /></td> </tr> </table> <button type="submit">Save Listings</button> </form>
The Javascript is:
<script type="text/javascript"> var viewModel = {}; $.getJSON('/Task/CreateJson', function (result) { viewModel = ko.mapping.fromJS(result.Data); viewModel.save = function () { var data = ko.toJSON(this); $.ajax({ url: '/Task/Create', contentType: 'application/json', type: "POST", data: data, dataType: 'json', success: function (result) { ko.mapping.updateFromJS(viewModel, result); } }); } ko.applyBindings(viewModel); }); </script>
JSON from Fiddler that gets loaded into the page as below.
{ "ContentEncoding":null, "ContentType":null, "Data":{ "Task":{ "ReferenceTypeId":0, "Reference":"Default Value" }, "ReferenceTypes":[ { "Id":2, "Name":"A Ref Type" }, { "Id":3, "Name":"B Ref Type" }, { "Id":1, "Name":"C Ref Type" } ] }, "JsonRequestBehavior":1 }
This comes back into the server (ASP.NET MVC3) correctly, with the updated Reference string value, but ReferenceTypeId is not bound to the correctly selected drop down value. Do I need to perform any additional functions to bind correctly etc? Or tell the data-bind what the select value column is (Id) etc? I have checked in Fiddler on the values getting posted back from the browser, and it has the same original value (0). So it is definately not the server.
I hope someone can help, if you need any further information please ask.
Kind Regards Phil
原文:https://stackoverflow.com/questions/9746042
最满意答案
你可以使用
toeplitz
生成一个包含需要添加到原始矩阵中的值的矩阵:M = [5 5 5; 7 7 7; 9 9 9]; %// data matrix v = [1 11 4 3 2]; %// data vector S = toeplitz(v); S = S(1:(numel(v)+1)/2, (numel(v)+1)/2:end); result = M+S;
或者,如@thewaywewalk所述,您可以更直接地进行如下操作:
M = [5 5 5; 7 7 7; 9 9 9]; %// data matrix v = [1 11 4 3 2]; %// data vector result = M + toeplitz(v(size(M,1):-1:1), v(size(M,2):end));
You can use
toeplitz
to generate a matrix containing the values that need to be added to your original matrix:M = [5 5 5; 7 7 7; 9 9 9]; %// data matrix v = [1 11 4 3 2]; %// data vector S = toeplitz(v); S = S(1:(numel(v)+1)/2, (numel(v)+1)/2:end); result = M+S;
Or, as noted by @thewaywewalk, you can do this more directly as follows:
M = [5 5 5; 7 7 7; 9 9 9]; %// data matrix v = [1 11 4 3 2]; %// data vector result = M + toeplitz(v(size(M,1):-1:1), v(size(M,2):end));
相关问答
更多-
元素的mutiplication。*向量在Matlab中给出矩阵(Element-wise mutiplication .* of vectors gives matrix in Matlab)[2023-01-10]
由于隐式扩展( 在2016b中引入 ),它与使用bsxfun基本相同。 但这意味着什么? 建立: a = 1:3; b = 2:4; 所有MATLAB版本: c = a.*b; % c = [2 6 12], element-wise multiplication c(j) = a(j)*b(j) c = b'*a; % c = [2 4 5; 3 6 9; 4 8 12] % standard matrix multiplication of vectors % c(i,j) = a(i) + ... -
如果我理解正确,那么你想要每个对角线的累积最大值。 使用cummax和两个for循环,您可以得到您想要的: A[row(A)==col(A)] <- cummax(A[row(A)==col(A)]) for(i in 1:(nrow(A)-1)) { A[row(A)==col(A)-i] <- cummax(A[row(A)==col(A)-i]) } for(i in 1:(ncol(A)-1)) { A[row(A)-i==col(A)] <- cummax(A[row(A)-i==co ...
-
用这个 - out = X./Y %// Perform the elementwise division out(Y==0)=0 %// Select the positions where Y is zero and %// set those positions in the output to zero 输出 - X = 1 2 3 4 5 6 Y = 0 1 -1 ...
-
使用matlab中的逐元加法将值添加到矩阵的对角线(adding values to diagonals of matrix using element-wise addition in matlab)[2023-06-11]
你可以使用toeplitz生成一个包含需要添加到原始矩阵中的值的矩阵: M = [5 5 5; 7 7 7; 9 9 9]; %// data matrix v = [1 11 4 3 2]; %// data vector S = toeplitz(v); S = S(1:(numel(v)+1)/2, (numel(v)+1)/2:end); result = M+S; 或者,如@thewaywewalk所述,您可以更直接地进行如下操作: M = [5 5 5; 7 7 7; 9 9 9]; %// ... -
matlab中有效的密集对角矩阵生成[复制](Efficient dense diagonal matrix generation in matlab [duplicate])[2023-10-08]
使用链接问题中给出的方法: a = [conj(data(end:-1:2)), data]; n = (numel(a)+1)/2; A = a(bsxfun(@minus, n+1:n+n, (1:n).')); Using the approach given in the linked question: a = [conj(data(end:-1:2)), data]; n = (numel(a)+1)/2; A = a(bsxfun(@minus, n+1:n+n, (1:n).')); -
矩阵加法跳过NaN值(matrix addition skipping NaN values)[2023-07-25]
你仍然可以使用nansum,如果你在n + 1维上连接你的nd数组。 对于2D % commands de-nested for readability. You can do this with a single line, of course tmp = cat(3,a,b); c = nansum(tmp,3); 一般情况 function out = nansumByElement(A,B) %NANSUMBYELEMENT performs an element-wise nansum on t ... -
使用OpenCV的元素功率(Element-wise power using OpenCV)[2022-12-24]
您可以通过将src1 , src2和dst转换为float (CV_32F)类型的矩阵来实现此功能。 这是因为代码在float* ptrs[3];以这种方式处理它们float* ptrs[3]; 。 最后给出了使用opencv函数log , multiply和exp的替代实现。 作为第二个问题的例子, Mat src1 = (Mat_(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9); Mat src2 = (Mat_ (3, 3) << ... -
这里我们要为非零条目添加一个标量,并单独留下矩阵稀疏性,即不要触摸零条目。 从精细的Scipy文档 ( ** emphasis **是我的): Attributes nnz Get the count of explicitly-stored values (nonzeros) has_sorted_indices Determine whether the matrix has sorted indices dtype (dtype) D ...
-
matlab提取三维矩阵对角线的有效方法[重复](matlab efficient way to extract diagonals of a 3D matrix [duplicate])[2022-02-01]
您可以。 如果将Sigma重塑为矩阵,则选择3D矩阵的对角线现在从矩阵中选择行。 Sigma3=reshape(Sigma,[],size(Sigma,3)); Selector=diag(true(size(Sigma,1),1)); Sigma2=Sigma3(Selector(:),:); %Sigma2=permute(Sigma2,[3,1,2]) %optional last step to get a result with the same dimensions. You can. If ... -
您可以创建相应的Hankel矩阵并将其用于排序(仅在输出为方阵时才起作用!): x = [1 2 3 4 5 6 7 8 9]; %# find size of output (works only with square arrays) n=sqrt(length(x)); %# create Hankel matrix hh = hankel(1:n,n:(2*n-1)); %# sort to get order of elements (conveniently, sort doesn't d ...