jquery-asp.net mvc:自定义ViewModel表单发布

刚开始使用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的好文章.

我认为您需要编写自己的活页夹,因为您试图构建一个复杂类型的数组.本身就可以使用复杂类型,这是问题在数组中出现的地方.

祝好运!

相关文章

1.第一步 设置响应头 header(&#39;Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...