开发者

One View and Many Models inside Model Container

Hello guys I have a little trouble with the my method [HTTPost], please see my code below:

MODELS (models Case and Person have more attributes than those presented here)

public class Case
     {
            public string caseNumber
            {
                get;
                set;
            }
        }


    public class Person
        {
            public string name
            {
                get;
                set;
            }

            public string lastName
            {
                get;
                set;
            }
        }


    public class ModelViewContainer {

            p开发者_开发技巧ublic Person Person { 
                get;
                private set;
            }

    public Case Case{ 
                get;
                private set;
            }

    public ModelViewContainer (Person person, Case casep)
            {

                Persona = persona;
                Case = casep;

            }

        }

IN MY CONTROLLER

        public ActionResult PresentView() {

            return View(new ModelViewContainer(new Person(),new Case()));
        }


        [HttpPost]    
        public ActionResult PresentView(ModelViewContainer model)  **<--never enters here**
        {
            if (ModelState.IsValid){
            ....
            }
            else
            return View(model);
        }

VIEW

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.ModelViewContainer>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

            Name
                <%: Html.TextBoxFor(model => model.Persona.nombre)%>
                <%: Html.ValidationMessageFor(model => model.Persona.nombre)%>

            LastName
                <%: Html.TextBoxFor(model => model.Persona.apellido)%>
                <%: Html.ValidationMessageFor(model => model.Persona.apellido)%>
            Case Number
                <%: Html.TextBoxFor(model => model.Case.caseNumber)%>
                <%: Html.ValidationMessageFor(model => model.Case.caseNumber)%>
            <p>
                <input type="submit" value="Create" />
            </p>

    <% } %>

</asp:Content>

The result error is: System.MissingMethodException: No parameter less constructor defined for this object.

Is it possible to capture the model in httpost or not? If not, how can i do? (Excuse my English) Please help me.

Edit-- Corrections

public class ModelViewContainer {

            public Person Person { 
                get;
                set; //not private
            }

    public Case Case{ 
                get;
                set; //not private
            }

    public ModelViewContainer (Person person, Case casep)
            {

                Persona = persona;
                Case = casep;

            }

    public ModelViewContainer (){ //add parammeter less constructor
            }



        }


ASP.Net MVC doesn't work with immutable models; it doesn't realize that it can just pass the property values as constructor parameters.

Your ModelViewContainer needs a parameter-less constructor and writable properties.

Alternatively, you could write your own ModelBinder that knows how to do that. (I do not recommend this option)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜