0
votes

I am using a ViewModel class to bind a data entry page, the hierarchy of the view model class is like this

public ClassA
{
    public SomeModelType1 MyProperty1 { get; set; }
    public List<SomeModelType2> MyProperty2 { get; set; }
}

public class SomeModelType1 
{
    public string Name { get; set; }
    public SomeModelType3 MyProperty13 { get; set; }
}
public class SomeModelType3
{
    public int Id { get; set; }
}

When I bind Class A to a View, I bind the textboxes as follows:

Model.MyProperty1.Name)%> and Model.MyProperty1.SomeModelType3 .Id, new { @id = "hdfId" })%>

but when I submit the form I have written , [AcceptVerbs(HttpVerbs.Post)] public ActionResult BuildProfile(ClassA a) { //some logic }

so when I debug the a.MyProperty1 value I am always getting MyProperty1 as null value, can please some body help? I do not understand where am i going wrong.


GET METHOD

 [AcceptVerbs(HttpVerbs.Get)]
public ActionResult BuildProfile(Int32 UserId, Int32 CompanyDentistId, Int32 AccountId)
{
A objProfile = new A ();          
objProfile.CurrentStep = 1;
objProfile.MyProperty1.MyProperty13.CompanyId = CompanyId;
objProfile.MyProperty1.MyProperty13.AccountId = AccountId;
objProfile.MyProperty1.UserId = UserId;
return View(objProfile);
}

POST METHOD

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult BuildProfile(BuildProfileViewModel a)
    {          
        return RedirectToAction("BuildProfile", new { UserId = a.MyProperty1.UserId, CompanyId = a.MyProperty1.MyProperty13.CompanyId, AccountId = a.MyProperty1.MyProperty13.AccountId });

}

VIEW

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/DefaultMaster.Master"
Inherits="System.Web.Mvc.ViewPage<ChooseYourDentist.ViewModel.BuildProfileViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"
    type="text/javascript"></script>
<script type="text/javascript">
    var stepCounter = 1;
    var TotalSteps = 4;
    function IntializeLoader() {

        $("#divAJAXLoader").dialog({
            autoOpen: false,
            height: 50,
            width: 100,
            modal: true,
            resizable: false,
            draggable: false
            //position: ["center", "center"],
            // , autoOpen: false
        });
        $(".ui-dialog-titlebar").hide();
    }
    function OnSuccess(data) {
        if (data.result == 1) {
            var UserId = data.userid;
            // window.location.href = '/Dentist/ViewCompanyProfile?UserID=' + UserId + '';
        }
        else {
            alert("Some error occured while trying to save the data, Please try again.");
        }
    }
    function OnBegin() {
        $("#divAJAXLoader").dialog("open");
        return CompareDates($("#FromDate").val(), $("#ToDate").val());
    }
    function OnComplete() {
        $("#divAJAXLoader").dialog("close");
    }
</script>

<% using (Html.BeginForm("BuildProfile", "Dentist", Model, FormMethod.Post, new { dmodel = Model, @id = "frmBuildProfile" }))
   {
       ChooseYourDentist.Models.UserAccountModel obj1 = new ChooseYourDentist.Models.UserAccountModel();
       this.Model.MyProperty1 = obj1;
       %>
<div>
    <h5>
        Add/Edit<span> Doctor Profile</span></h5>
    <div class="req_an_process">
        <div class="build_p_block">
            <div class="progress_bar">
                <div class="req_selected">
                    Basic Profile
                </div>
                <div class="req_deselected">
                    Dentist Profile
                </div>
                <div class="req_deselected_last">
                    Gallery
                </div>
                <div class="clear">
                </div>
            </div>
            <div class="form_content" id="First">
                <h4>
                    Basic Profile</h4>
                <p>
                    ChooseYourDentist.com makes it easy for dentists and patients to connect. Simply
                    complete the information below and you'll be on your way to connecting with more
                    patients.</p>
                <ul>
                    <li>First Name:</li>
                    <li class="mar_bot">
                        <%: Html.TextBoxFor(model => model.MyProperty1.FirstName)%>
                        <%: Html.ValidationMessageFor(model => model.MyProperty1.FirstName)%></li>
                    <li>Last Name:</li>
                    <li class="mar_bot">
                        <%: Html.TextBoxFor(m=> Model.MyProperty1.LastName)%>
                        <%: Html.ValidationMessageFor(m=> Model.MyProperty1.LastName)%></li>
                    <li>Gender:</li>
                    <li class="mar_bot">
                        <%: Html.RadioButtonFor(model=>Model.MyProperty1.MyProperty13.Gender,"Male", new { id = "rbtnMale" }) %>Male
                        <%: Html.RadioButtonFor(model=>Model.MyProperty1.MyProperty13.Gender,"Female", new { id = "rbtnFemale" }) %>Female
                    </li>
                    <li>Title:</li>
                    <li class="mar_bot">
                        <%= Html.DropDownListFor(m=> Model.MyProperty1.MyProperty13.TitleId, new SelectList((IEnumerable)ViewData["lstTitle"], "ReferenceDataId", "ReferenceDataName"), "-Select Title-", new { @id = "ddlTitle" })%>
                    </li>

                    <li class="mar_bot">
                        <input type="submit" class="green_btn" value="Save" name="Save" />

                    </li>
                </ul>
            </div>
            <div class="form_content" id="second" style="display: none;">
                <h4>
                    Dentist Profile</h4>
            </div>
        </div>
    </div>
</div>
<%=Html.HiddenFor(m=> Model.CurrentStep, new { @id = "hdfCurrentStep" })%>
<%=Html.HiddenFor(m=> Model.TotalSteps, new { @id = "hdfTotalSteps" })%>
<%=Html.HiddenFor(m=> Model.MyProperty1.UserId, new { @id = "hdfUserId" })%>
<%=Html.HiddenFor(m=> Model.MyProperty1.MyProperty13.CompanyId, new { @id = "hdfCompanyId" })%>
<%=Html.HiddenFor(m=> Model.MyProperty1.MyProperty13.AccountId, new { @id = "hdfAccountId" })%>
<% } %>
<div id="divAJAXLoader" style="display: none; text-align: center;">
    <img src="<%: Url.Content("~/Content/images/indicator_white.gif") %>" alt="Loading..." />
</div>

2

2 Answers

0
votes

In all your views that you bind back, every class used by the view needs a parameterless constructor. You also need to instantiate every class you are using in your views in that constructor.

public ClassA
{
    public ClassA(){
        MyProperty1 = new SomeModelType1();
        MyProperty2 = new List<SomeModelType2>();
    }

    public SomeModelType1 MyProperty1 { get; set; }
    public List<SomeModelType2> MyProperty2 { get; set; }
}

public class SomeModelType1 
{
    public SomeModelType1(){
        MyProperty13 = new SomeModelType3();
    } 

    public string Name { get; set; }
    public SomeModelType3 MyProperty13 { get; set; }
}
public class SomeModelType3
{
    public int Id { get; set; }
}
0
votes

According to me, the scope of inheritance in MVC models is limited to 2 or less inheritance, not abuse

public ClassA
{


//properties of SomeModelType1
public string Name { get; set; }
//propperties of SomeModelType3
public int Id { get; set; }
//if you need a list of int
//public list<int> Id   


//missing SomeModelType2
public List<SomeModelType2> MyProperty2 { get; set; }   
}