Asp.net mvc MVC复选框获取
我试图实现一个带有多个复选框的搜索面板来过滤表数据,但我遇到了一个问题。提交后,我无法保留选中的输入值 我怎样才能解决这个问题 我的模型:Asp.net mvc MVC复选框获取,asp.net-mvc,checkbox,filter,Asp.net Mvc,Checkbox,Filter,我试图实现一个带有多个复选框的搜索面板来过滤表数据,但我遇到了一个问题。提交后,我无法保留选中的输入值 我怎样才能解决这个问题 我的模型: public class OrdineView { public int anno { get; set; } public Int32 nrOrdine { get; set; } public string centro { get; set; } [DataType(DataType.Date), DisplayForm
public class OrdineView
{
public int anno { get; set; }
public Int32 nrOrdine { get; set; }
public string centro { get; set; }
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? data { get; set; }
public String codice { get; set; }
public String ragsoc { get; set; }
[DisplayFormat(DataFormatString = "{0:C}")]
public Nullable<double> importo;
}
在我看来,我有
<input name="Distinzione" type="checkbox" value="001">001
<input name="Distinzione" type="checkbox" value="002">002
001
002
……等等
提交后,我正确获取了数据,但丢失了选中状态。更新:根据注释,我更新了视图并添加了更多代码 如果您的意思是在页面刷新后复选框不保持选中状态。这是因为你没有告诉他们哪些应该检查。你有一个可能的解决办法。在需要复选框的视图中创建一个简单的帮助器方法。这个方法只检查值数组,如果它在数组中找到值,它将呈现一个状态为checked的复选框 View.cshtml 或者,如果您不想(或不能)创建新的视图模型(但我强烈建议您这样做)。您可以使用ViewBag传递检查值的附加集合:
public ActionResult Index(OrdiniSearchModel searchModel, int? pageNumber)
{
ViewBag.Distinzione = searchModel.Distinzione;
// original code
}
然后您只需更新helper方法。为了简单起见,我不检查ViewBag.Distinzione是否存在。但是你应该
@helper MyCheckbox(string value)
{
if (ViewBag.Distinzione.Contains(value))
{
<input type="checkbox" name="Distinzione" value="@value" checked="checked"/>
}
else
{
<input type="checkbox" name="Distinzione" value="@value" />
}
@value
}
@helper MyCheckbox(字符串值)
{
if(ViewBag.Distinzione.Contains(值))
{
}
其他的
{
}
@价值观
}
简而言之。您需要确保控制器中获得的数据(检查值的集合)被发送回视图。List distinzioniSelezionate=new List();
List<string> distinzioniSelezionate = new List<string>();
if (searchModel.distinzione != null && searchModel.distinzione.Count() > 0)
{
foreach (var item in searchModel.distinzione)
{
distinzioniSelezionate.Add(item);
}
}
OrdinePagedList model = new OrdinePagedList
{
Pages = pages.OrderBy(i => i.Codice).ToPagedList(pageNumber ?? 1, 10),
Distinzione = distinzioniSelezionate
};
if(searchModel.distinzione!=null&&searchModel.distinzione.Count()>0)
{
foreach(searchModel.distinzione中的var项)
{
添加(项目);
}
}
OrdinePagedList模型=新OrdinePagedList
{
Pages=Pages.OrderBy(i=>i.Codice).ToPagedList(页码??1,10),
Distinzione=distinzioniSelezionate
};
我不得不修改ActionResult,因为Distinzione不是空的,因为您手动为复选框生成html,而不会为您提供模型绑定。请说明如何解决此问题?我也使用了html.Checkbox,但没有任何变化。如何解决这类问题…我是一个新手,需要很多答案…但我的观点有一个模型@model IPagedList。我怎样才能解决这个问题?无论如何tksI忘了。。。是的,问题是,现在我不能尝试你的建议,但你打开我的心…我相信它会工作。又来了。任何问题我都会告诉你。抱歉用英语。我试图编辑ActionResult以获得选中的“Distinzione”,并以某种方式使用helper works。但我对你们新课程的解决方案很感兴趣。我试图实现视图,但无法迭代页面字段。如何使用@model或nepagelist?我也试过用IEnumerable!“I get OrdinePagedList”不包含“Anno”的定义,并且找不到接受类型为“OrdinePagedList”的第一个参数的扩展方法“Anno”(是否缺少using指令或程序集引用?)
public class OrdinePagedList
{
public IEnumerable<OrdiniView> Pages { get; set; }
public IEnumerable<string> Distinzione { get; set;
}
// from
public IQueryable<OrdineView> GetOrdini(OrdiniSearchModel ordiniSearch)
// to
public OrdinePagedList GetOrdini(OrdiniSearchModel ordiniSearch)
public ActionResult Index(OrdiniSearchModel searchModel, int? pageNumber )
{
ViewBag.Anno = db.ORDINI.Select(o => new { o.Anno }).Distinct().OrderByDescending(o => o.Anno).Select(o => o.Anno);
var searchLogic = new OrdiniBusinessLogic();
var pages = searchLogic.GetOrdini(searchModel);
OrdinePagedList model = new OrdiniPagedList {
Pages = pages.OrderBy(i => i.codice).ToPagedList(pageNumber ?? 1, 10),
Distinzione = searchModel.Distinzione
}
return View(model);
}
public ActionResult Index(OrdiniSearchModel searchModel, int? pageNumber)
{
ViewBag.Distinzione = searchModel.Distinzione;
// original code
}
@helper MyCheckbox(string value)
{
if (ViewBag.Distinzione.Contains(value))
{
<input type="checkbox" name="Distinzione" value="@value" checked="checked"/>
}
else
{
<input type="checkbox" name="Distinzione" value="@value" />
}
@value
}
List<string> distinzioniSelezionate = new List<string>();
if (searchModel.distinzione != null && searchModel.distinzione.Count() > 0)
{
foreach (var item in searchModel.distinzione)
{
distinzioniSelezionate.Add(item);
}
}
OrdinePagedList model = new OrdinePagedList
{
Pages = pages.OrderBy(i => i.Codice).ToPagedList(pageNumber ?? 1, 10),
Distinzione = distinzioniSelezionate
};