0

ViewModel クラスを使用してデータ入力ページをバインドしています。ビュー モデル クラスの階層は次のようになります。

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; }
}

クラス A をビューにバインドすると、次のようにテキスト ボックスがバインドされます。

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

しかし、私が書いたフォームを送信すると、 [AcceptVerbs(HttpVerbs.Post)] public ActionResult BuildProfile(ClassA a) { //some logic }

そのため、a.MyProperty1値をデバッグすると、常に MyProperty1 が null 値として取得されます。誰か助けてください。どこが間違っているのかわかりません。


メソッドを取得

 [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);
}

投稿方法

[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 });

}

見る

<%@ 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>

4

2 に答える 2

0

バインドし直すすべてのビューでは、ビューで使用されるすべてのクラスにパラメーターなしのコンストラクターが必要です。また、そのコンストラクターのビューで使用しているすべてのクラスをインスタンス化する必要があります。

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; }
}
于 2013-02-22T15:23:47.680 に答える