mvc3 razor引擎中的多个复选框值 [英] Multiple checkboxes values in mvc3 razor engine

查看:96
本文介绍了mvc3 razor引擎中的多个复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图中有一些文本框和多个复选框,所以我必须插入多个选中的复选框值存储到数据库中并检索那些更新..我搜索了很多网站,但没有人给出完美的解决方案所以请帮助我。简要说明从视图到模型和数据库的流程。

In my view some text boxes and multiple check boxes are there so i have to insert multiple selected check boxes values stored into database and retrieve those for update..I have searched lot of sites but no one give perfect solution so please help me. explain briefly flow from view to model and database for that.

推荐答案

您好,您可以使用此代码: -



在视图中: -



@using(Html.BeginForm(Index,Home))

{

foreach(模型中的var项目)

{

@ Html.CheckBox(category+ item.Id,true)

}



}



现在在控制器中: -



公共ActionResult索引(FormCollection集合)

{

foreach(var key in collection.Keys)

{

if(key.ToString()。StartsWith(category))

{

int itemId = int.Parse(key。 ToString()。替换(category,));



//有时键返回on和offinstreadtrue和假

if(集合[key.ToString()]。包含(true))

{



// CheckBox被选中

//在这里用ItemId做一些事情

}

其他

{

// checkBox未选中

}

}

}

返回RedirectToAction(ManageArticleCategory);

}
Hi you can use this code:-

In view:-

@using (Html.BeginForm("Index","Home"))
{
foreach(var items in model)
{
@Html.CheckBox("category" + item.Id, true)
}

}

And now in controller:-

public ActionResult Index(FormCollection collection)
{
foreach (var key in collection.Keys)
{
if (key.ToString().StartsWith("category"))
{
int itemId= int.Parse(key.ToString().Replace("category", ""));

// sometime key return "on" and "off" instread of "true" and "false"
if (collection[key.ToString()].Contains("true"))
{

//CheckBox is checked
//Do something here with ItemId
}
else
{
//checkBox not checked
}
}
}
return RedirectToAction("ManageArticleCategory");
}


这篇关于mvc3 razor引擎中的多个复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆