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

前端之家收集整理的这篇文章主要介绍了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的好文章.

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

祝好运!

原文链接:https://www.f2er.com/jquery/530920.html

猜你在找的jQuery相关文章