这是我的json结果动作的视图,它包含属性ID,IsChecked复选框和Propery title列。
在此视图中,我可以选中或取消选中这些复选框。
选择特定复选框并单击创建手册后,我想将这些值传递给另一个控制器方法。
例如:假设我先检查了两个结果
#include <iostream>
#include <boost/proto/proto.hpp>
namespace mpl = boost::mpl;
namespace proto = boost::proto;
namespace LinAlg {
class Vector;
class Matrix;
// Callable transform object to make a proto exression
// for lazily evaluationg a. multiplication
struct MatVecMult;
// The grammar for the multiplication of a matrix and a vector
struct MatVecMultGrammar : proto::or_<
proto::when<
proto::multiplies< proto::terminal< Matrix> ,
proto::terminal< Vector> >,
MatVecMult( proto::_value( proto::_left),
proto::_value( proto::_right) )
>
> {};
// The grammar for a vector expression
// ( Now I consider just one form : matrix * vector . )
struct VecExprGrammar : proto::or_<
proto::function< MatVecMultGrammar, proto::_>,
MatVecMultGrammar
> {};
// A wrapper for a linear algebraic expression
template<typename E> struct Expr;
// The above grammar is associated with this domain.
struct Domain
: proto::domain<proto::generator<Expr>, VecExprGrammar>
{};
// A wrapper template for linear algebraic expressions
// including matrices and vectors
template<typename ExprType>
struct Expr
: proto::extends<ExprType, Expr<ExprType>, Domain>
{
/* typedef double result_type; */
explicit Expr(const ExprType& e)
: proto::extends<ExprType, Expr<ExprType>, Domain>(e)
{}
};
// Testing if data in an heap array can be a vector object
class Vector {
private:
int sz;
double* data;
public:
explicit Vector(int sz_ = 1, double iniVal = 0.0) :
sz( sz_), data( new double[sz] ) {
for (int i = 0; i < sz; i++) data[i] = iniVal;
std::cout << "Created" << std::endl;
}
Vector(const Vector& vec) :
sz( vec.sz), data( new double[sz] ) {
for (int i = 0; i < sz; i++) data[i] = vec.data[i];
std::cout << "Copied! " << std::endl;
}
~Vector() {
delete [] data;
std::cout << "Deleted" << std::endl;
}
// accessing to an element of this vector
double& operator()(int i) { return data[i]; }
const double& operator()(int i) const { return data[i]; }
};
// Testing if data in an heap array can be a matrix object
class Matrix
{
private:
int rowSz, colSz;
double* data;
double** m;
public:
explicit Matrix(int rowSize = 1, int columnSize =1,
double iniVal = 0.0) :
rowSz( rowSize), colSz(columnSize),
data( new double[rowSz*colSz] ), m( new double*[rowSz])
{
for (int i = 0; i < rowSz; i++) m[i] = data + i*colSz;
for (int ri = 0; ri < rowSz; ri++)
for (int ci = 0; ci < colSz; ci++) m[ri][ci] = iniVal;
std::cout << "Created" << std::endl;
}
Matrix(const Matrix& mat) :
rowSz( mat.rowSz), colSz( mat.colSz),
data( new double[rowSz*colSz] ), m( new double*[rowSz])
{
for (int i = 0; i < rowSz; i++) m[i] = data + i*colSz;
for (int ri = 0; ri < rowSz; ri++)
for (int ci = 0; ci < colSz; ci++)
m[ri][ci] = mat.m[ri][ci];
std::cout << "Copied! " << std::endl;
}
~Matrix()
{
delete [] m;
delete [] data;
std::cout << "Deleted" << std::endl;
}
int rowSize() const { return rowSz; }
int columnSize() const { return colSz; }
// accesing to a vector element
double& operator()(int ri, int ci) { return m[ri][ci]; }
const double& operator()(int ri, int ci) const { return m[ri][ci]; }
};
// Lazy function object for evaluating an element of
// the resultant vector from the multiplication of
// a matrix and vector objects.
//
// An expression like ( matrix * vector )(index) is transformed
// into the loop for calculating the dot product between
// the index'th row of the matrix and the vector.
struct LazyMatVecMult
{
Matrix const& m;
Vector const& v;
int mColSz;
typedef double result_type;
// typedef mpl::int_<1> proto_arity;
explicit LazyMatVecMult(Matrix const& mat, Vector const& vec) :
m( mat), v( vec), mColSz(mat.rowSize()) {}
LazyMatVecMult(LazyMatVecMult const& lazy) :
m(lazy.m), v(lazy.v), mColSz(lazy.mColSz) {}
result_type operator()(int index) const
{
result_type elm = 0.0;
for (int ci =0; ci < mColSz; ci++)
elm += m(index, ci) * v(ci);
return elm;
}
};
// Callable transform object to make the lazy functor
// a proto exression for lazily evaluationg the multiplication
// of a matrix and a vector .
struct MatVecMult : proto::callable
{
typedef proto::terminal< LazyMatVecMult >::type result_type;
result_type
operator()( Matrix const& mat, Vector const& vec) const
{
return proto::as_expr( LazyMatVecMult(mat, vec) );
}
};
// Define a trait for detecting linear algebraic terminals, to be used
// by the BOOST_PROTO_DEFINE_OPERATORS macro below.
template<typename> struct IsExpr : mpl::false_ {};
template<> struct IsExpr< Vector> : mpl::true_ {};
template<> struct IsExpr< Matrix> : mpl::true_ {};
// template<> struct IsExpr< MatVecMult> : mpl::true_ {};
template<> struct IsExpr< LazyMatVecMult> : mpl::true_ {};
// This defines all the overloads to make expressions involving
// Vector and Matrix objects to build Proto's expression templates.
BOOST_PROTO_DEFINE_OPERATORS(IsExpr, Domain)
// BOOST_PROTO_DEFINE_OPERATORS(IsExpr, proto::default_domain)
}
int main()
{
using namespace LinAlg;
proto::_default<> trans;
Matrix mat( 3, 3);
Vector vec1(3), vec2(3);
mat(0,0) = 1.00; mat(0,1) = 1.01; mat(0,2) = 1.02;
mat(1,0) = 1.10; mat(1,1) = 1.11; mat(1,2) = 1.12;
mat(2,0) = 1.20; mat(2,1) = 1.21; mat(2,2) = 1.22;
vec1(0) = 1.0;
vec1(1) = 2.0;
vec1(2) = 3.0;
std::cout << " mat * vec1" << std::endl;
proto::display_expr( mat * vec1 );
std::cout << " MatVecMultGrammar()( mat * vec1) " << std::endl;
proto::display_expr( MatVecMultGrammar()( mat * vec1) );
std::cout << "( mat * vec1)(2)" << std::endl;
proto::display_expr( ( mat * vec1)(2) );
std::cout << "VecExprGrammar()( ( mat * vec1)(2) )" << std::endl;
proto::display_expr( VecExprGrammar()( ( mat * vec1)(2) ) );
double elm2 = trans( VecExprGrammar()( ( mat * vec1)(2) ) );
std::cout << "( mat * vec1)(2) = " << elm2 << std::endl;
return 0;
}
我想在点击后将上述值发送到另一个控制器,我该怎么做。
这是HTML代码
property ID | IsChecked
1 | True
2 | True
我有以下json脚本从数据表中检索数据
<table class="table">
<thead>
<tr>
<th>Property ID</th>
<th>IsChecked</th>
<th>Property Tile</th>
</tr>
</thead>
<tbody id="table"></tbody>
</table>
<table id="template" class="table" style="display: none;">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<div style="width:50%; float:left;text-align:left"><button id="resetborchure" type="button" class="btn btn-warning submit">Reset Brochure</button> </div>
<div style="width:50%; float:left;text-align:right"><button id="createborchure" type="button" class="btn btn-danger submit" onclick="location.href='@Url.Action("Create_Brochure", "Brochure")'">Create Brochure</button> </div>
答案 0 :(得分:2)
您尚未展示您的模型或FetchProductProperties()
正在做什么,但假设它正在创建一个对象集合(比如)List<ProductPropertyVM>
,其中ProductPropertyVM
包含属性int ID
,string Title
和bool IsChecked
然后处理这个问题的简单方法是返回部分视图而不是json。例如在控制器中
public ActionResult FetchProductProperties(...)
{
List<ProductPropertyVM> data = ..... // your query
return PartialView("_ProductProperty", data);
}
和_ProductProperty.cshtml
部分视图
@model List<ProductPropertyVM>
<table>
... // thead elements
<tbody>
@for(int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(m => m[i].ID)
@Html.HiddenFor(m => m[i].ID)
</td>
<td>@Html.CheckBoxFor(m => m[i].IsChecked)</td>
<td>@Html.DisplayFor(m => m[i].Title)</td>
</tr>
}
</tbody>
</table>
.... // buttons
然后在主视图中包含一个占位符来呈现部分
<div id="productproperties"></div>
并将脚本修改为
$('#search').click(function () {
$('#productproperties').load(url, { .... }); // add data to be passed to the method as required
});
如果你想通过返回json来做到这一点 - 即方法有return Json(data, JsonRequestBehavior.AllowGet);
,那么你需要使用正确的名称属性(包括索引器)生成控件。每行的html都需要(其中#
是索引器 - 从零开始,?
是json数据的值)
<tr>
<td>
<span> // value of ID property </span>
<input type="hidden" name="[#].ID value="?" />
</td>
<td>
<input type="checkbox" name="[#].IsChecked" value="true" />
<input type="hidden" name="[#].IsChecked" value="false" />
</td>
<td>
<span> value of Title property </span>
</td>
</tr>
生成此内容的最简单方法是将其作为隐藏div
(例如<div id="template">
)内部和form
元素之外的模板。然后在$.each()
函数中克隆模板,并更新值,包括更新索引器。
$.each(data, function (index, item) {
var clone = $('#template').clone();
// update indexers
clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
// update input values as display text
var cells = clone.find('td');
cells.eq(0).children('span').text(item.ID);
cells.eq(0).children('input').val(item.ID);
cells.eq(1).children('input').first().prop('checked', item.IsChecked)
cells.eq(2).children('span').text(item.Title);
table.append(clone.html());
});
请参阅此DotNetFiddle以获取json方法的示例
在这两种情况下,这将回发到具有参数List<ProductPropertyVM> model
答案 1 :(得分:1)
如果您想继续使用上面的代码,请按照以下步骤操作: 删除按钮点击属性。最好使用 ajax 。
<div style="width:50%; float:left;text-align:right"><button id="createborchure" type="button" class="btn btn-danger submit">Create Brochure</button> </div>
现在实施按钮单击属性,如下所示:
$('#createborchure').click(function () {
var data=[];
$('#table tr').each(function(){
// creating json data based on checked row.
$this=$(this)
var value=$this.find('input:checked').val();
if(value=="1"){
var row={"id":$(this).find("td").eq(0).html(),
"Ischecked":"true",
"title":$(this).find("td").eq(2).html()}
data.push(row);
}
});
// Now use Ajax to send data to controller
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Brochure/Create_Brochure",
data: data,
success: function (result) {
//do somthing here
}
});
});
希望这会有所帮助。