刚开始使用ASP.NET MVC并已经遇到了绊脚石.
情况是,我有一个自定义ViewModel传递给视图,其中包含要评级的项目列表(将使用jQuery star评级),因此这些是使用单选按钮帮助器创建的,具有相同的名称,只是不同的值,这些都没有问题.
但是,我绝对不知道如何将其实际恢复到我的操作的后期版本中.我只是收到“无参数构造函数”错误.我不想使用表单集合-我希望我的数据保持基于类.
是否有人必须做类似的事情?
非常感谢您的任何建议.
================================================== =====================
UPDATE(包括基本代码):
In the HomeController:
public class MyViewModel
{
public MyViewModel(List<Thing> things ) // Thing.cs contains properties name and rating
{
this.Things = things;
}
public List<Thing> Things { get; private set; }
}
public ActionResult Index()
{
List<Thing> things = new List<Thing>();
Thing t;
t = new Thing();
t.name = "One";
t.rating = 1;
things.Add(t);
t = new Thing();
t.name = "Two";
t.rating = 2;
things.Add(t);
return View(new MyViewModel(things));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index( MyViewModel vm)
{
return View();
}
and in the Index page ( Inherits="System.Web.Mvc.ViewPage<MyProject.Controllers.MyViewModel>" )
<% using (Html.BeginForm())
{%>
<ul>
<% for( int t = 0; t<Model.Things.Count; t++)
{%>
<li>
<% for (int i = 1; i < 6; i++)
{
MyProject.Thing thing = Model.Things[i];
%>
<%=Html.RadioButton(String.Format("Things[{0}]",t),i)%>
<% } %>
</li>
<% }%>
</ul>
<input type="submit" value="submit me" />
<% } %>
最佳答案
尝试对ViewModel使用无参数构造函数.同样,属性的名称需要与控件的名称/ ID匹配.
您可能需要简化一些,甚至编写自己的模型绑定程序.
模型活页夹及其用法的说明here.
关于编写模型活页夹here的好文章.
我认为您需要编写自己的活页夹,因为您试图构建一个复杂类型的数组.本身就可以使用复杂类型,这是问题在数组中出现的地方.
祝好运!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。