MVC Html.CheckBox列表和Jquery Post? [英] MVC Html.CheckBox list and Jquery Post ?
问题描述
我的代码是
my code is
<% using (Html.BeginForm())
{%>
<table>
<tr>
<th>
LabelID_FK
</th>
<th>
LabelName
</th>
<th>
LabelIsDocument
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%: item.LabelID_FK %>
</td>
<td>
<%: item.LabelName %>
</td>
<td>
<%-- <input type="checkbox" value="df" id="chk" onclick="check()" />--%>
<%=Html.CheckBox("chk_"+ item.LabelID_FK)%>
</td>
</tr>
<% } %>
</table>
<p>
<input type="button" value="submit" id="btn" />
</p>
其中显示文档标签的复选框列表,用户可以选择它.我想传递数据列表,该用户选择复选框,它使用jquery发布我的工作?
which show checkbox list for document label which user can select it . i want pass data list which user select checkbox it use jquery post what i do?
推荐答案
^ ]作为HTML Helper方法的MVC3复选框列表的示例.
Here''s[^] an example of a MVC3 checkboxlist as a Html Helper method.
在先前的解决方案注释中,我不理解您对jQuery的注释.
如果要在控件中接收数组(List< SomeClass>),则需要生成类似于以下内容的HTML:
I do not understand your comment about jQuery in previous solution comments.
If you want to receive an array (List<SomeClass>) in your control, you need to generate HTML similar to this:
<input name="MyArray[0].Role" value="MyKey" type="hidden" />
<input id="MyKey" name = "MyArray[0].Selected" type="checkbox" checked = "checked" value="true" />
<input name="MyArray[0].Selected" value="false" type="hidden" />
索引必须从0开始(至少在MVC 2中)并且必须是连续的.然后,您的SomeClass
通常具有一个属性MyKey
,它将标识该复选框,另一个属性Selected
将给出状态.
就我而言,由于我只对选中的项目感兴趣,因此似乎没有为未选中的项目设置密钥.
我没有在家中使用的源代码... HTML片段是由Internet Explorer中的查看源代码"找到的.
我认为通过在模型中有一个列表并正确地呈现带有嵌套名称的局部视图,可能有一种更简单的方法.但是我还没有尝试过.
Indexes must start at 0 (at least with MVC 2) and must be consecutive. Your SomeClass
will typically then have a property MyKey
that would identify the checkbox and another property Selected
which will give the state.
In my case, since I was only interested in checked items, it seems that I do not set the key for unselected items.
I do not have source code I uses here at home... The HTML fragment was found by "View source code" in Internet Explorer.
I think there might be an easier way to do it by having a list in the model and properly render a partial view with nested names.... but I haven''t tried it yet.
我在用户单击按钮并使用非常好时使用此代码
i use this code when user click on button and work very good
[HttpPost]
public ActionResult DocumentLabel(FormCollection model)
{
for (int i = 0; i < model.Count; i++)
{
AriaCRMEntities aria = new AriaCRMEntities();
DocumentLabel label = new DocumentLabel();
string lbl = model[i].ToString();
string[] check = lbl.Split('','');
bool chk = Convert.ToBoolean(check[0]);
string name = model.Keys[i].ToString();
string[] n = name.Split(''_'');
string lblid = n[1];
if (chk)
{
label.LabelID_FK = Int32.Parse(lblid);
Guid id = Guid.NewGuid();
label.DocumentID_FK = id;
aria.DocumentLabels.AddObject(label);
aria.SaveChanges();
}
}
return Content("0ok");
}
但是我想要jquery post我需要数组复选框,选中它以将其传递给控制器吗?
but i want jquery post i need array check box whit select it to pass it to controller?
这篇关于MVC Html.CheckBox列表和Jquery Post?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!