在asp.net MVC 2中接收多选post数组
本文关键字:post 数组 asp net MVC | 更新日期: 2023-09-27 17:50:56
我是ASP的新手。NET MVC 2"answers"c#"。我研究了很多,但到目前为止真的找不到任何能解决我问题的东西。我在服务器端接收多选择字段时遇到了麻烦。我有一个html表单,有5个选择字段,可以由用户可选地使用。5个字段中有3个具有"Multiple"属性。下面是表单:
//file "formHtml1.ascx"
<div>
<select name="app" id="app">
<option value="">All</option>
<% foreach (My.Namespace.App app in Model.App) { %>
<option value="<%:app.Id %>"><%:app.Name %></option>
<% } %>
</select>
</div><div>
<select name="etp" id="etp">
<option value="">All</option>
<option value="1">EF</option>
<option value="2">EM</option>
</select>
</div><div>
<select name="esc[]" id="esc" multiple="multiple">
<% foreach (My.Namespace.Esc esc in Model.Esc) { %>
<option value="<%:esc.Id %>"><%:esc.Name %></option>
<% } %>
</select>
</div><div>
<select name="answers[]" id="answers" multiple="multiple">
<% foreach (My.Namespace.Ans ans in Model.Ans) { %>
<option value="<%:ans.Id %>"><%:ans.Name %></option>
<% } %>
</select>
</div><div>
<select name="ar[]" id="ar" multiple="multiple">
<% foreach (My.Namespace.Ar ar in Model.Ars) { %>
<option value="<%: ar.Id %>"><%: ar.Name %></option>
<% } %>
</select>
</div>
</div>
我的控制器通过一个模型接收表单(至少,这是它应该做的,我猜)
// file MyController.cs
[HttpPost]
public PartialViewResult MyAction(MyModel model)
{
try
{
MyClassBO bo = new MyClassBO();
MyDependency myDependency = bo.GerRelEqpHab(model.TpRel, model.AgpQA, model.AgpQE, model.FilterApp, model.FilterEtp, model.FilterEsc, model.FilterAns, model.FilterAr);
DataTable dataTable = myDependency.Table;
string nameRel = myDependency.NameRel;
model.NameRel = nameRel;
model.Registers = dataTable;
ViewData.Model = model;
return PartialView("Partials/GerRel");
}
catch (Exception e)
{
throw e;
}
}
这里是正在使用的模型:
// file "MyModel.cs"
public class MyModel
{
public DataTable Registers { get; set; }
public string TpRel { get; set; }
public string AgpQA { get; set; }
public string AgpQE { get; set; }
public int? FilterApp { get; set; }
public int? FilterEtp { get; set; }
public int?[] FilterEsc { get; set; }
public int?[] FilterAns { get; set; }
public int?[] FilterAr { get; set; }
public bool Printing { get; set; }
public bool DownloadXLS { get; set; }
public string NameRel { get; set; }
public Dictionary<string, string> Filters { get; set; }
public MyModel(string tipoRelatorio, string agrupamentoQtdAlunos, string agrupamentoQtdEscolas, int? filtroAplicacao, int? filtroEtapa, int?[] filtroEscola, int?[] filtroSerie, int?[] filtroArea, bool printing, bool downloadXLS)
{
this.TpRel = tipoRelatorio;
this.AgpQA = agrupamentoQtdAlunos;
this.AgpQE = agrupamentoQtdEscolas;
this.FilterApp = filtroAplicacao;
this.FilterEtp = filtroEtapa;
this.FilterEsc = filtroEscola;
this.FilterAns = filtroSerie;
this.FilterAr = filtroArea;
this.Printing = printing;
this.DownloadXLS = downloadXLS;
}
public MyModel(string tipoRelatorio, string agrupamentoQtdAlunos, string agrupamentoQtdEscolas, int? filtroAplicacao, int? filtroEtapa, int?[] filtroEscola, int?[] filtroSerie, int?[] filtroArea)
{
this.TpRel = tipoRelatorio;
this.AgpQA = agrupamentoQtdAlunos;
this.AgpQE = agrupamentoQtdEscolas;
this.FilterApp = filtroAplicacao;
this.FilterEtp = filtroEtapa;
this.FilterEsc = filtroEscola;
this.FilterAns = filtroSerie;
this.FilterAr = filtroArea;
}
}
当我编译代码时,一切运行正常。但是当我运行项目时,我得到以下异常没有为这个对象定义无参数构造函数。
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
我不知道怎么回事
当尝试创建一个没有任何无参数构造函数的类的实例时,会发生此错误,在本例中是MyModel类。类的两个构造函数都需要实例化参数。不确定这是否会工作,但也许你可以尝试添加一个无参数的构造函数到MyModel类。
类似的帖子在这里:ASP。. NET MVC:没有为这个对象定义无参数构造函数