在主视图中创建的c#对象传递给局部视图为空

时间:2016-09-02 07:54:01

标签: c# asp.net-mvc asp.net-4.5

C#,。Net 4.5,MVC 5

参考下面的XX1和XX2(主视图):

对象在主控制器中初始化。 (标题和Detial都是。)

我在XX1和XX2都添加了断点,以确认初始值仍然存在。

  • XX1没问题。初始化的值就在那里 传递给控制器​​并由控制器接收。
  • XX2有问题。初始值仍然存在且是 传递给,但控制器以某种方式接收到一个空对象。

为什么细节的控制器没有为它传递传递的参数。

型号:

public class SampleNonRoutine
{
    public HeaderNonRoutine SampleHeader { get; set; }
    public List<DetailNonRoutine> SampleDetail { get; set; }
    public string Comments { get; set; }
}

public class HeaderNonRoutine
{
    public string Division { get; set; }
    public string Name { get; set; }
    public string Telephone { get; set; }
    [DisplayName("Sample Title")]
    public string SampleTitle { get; set; }
    [DisplayName("Retain Sample for")]
    public int RetainSample { get; set; }
}

public class DetailNonRoutine
{
    public int ID { get; set; }
    [DisplayName("Sample Reference #")]
    public string SampleReference { get; set; }
    [DisplayName("Sample Test")]
    public string SamplesTested { get; set; }
    [DisplayName("Sample Assays")]
    public string SampleAssays { get; set; }
}

控制器:

父视图

public ActionResult NonRoutineSamples(string SaveSend)
    {
        SampleNonRoutine sample = new SampleNonRoutine();
        sample.SampleHeader = new HeaderNonRoutine();
        sample.SampleDetail = new List<DetailNonRoutine>();
        sample.Comments = "Toets";

        for (int i = 0; i < 10; i++)
        {
            sample.SampleDetail.Add(new DetailNonRoutine { ID = i + 1, SampleReference = "", SamplesTested = "", SampleAssays = "" });
        }

        return View(sample);
    }

部分观点:

标题(部分视图):

    public ActionResult _HeaderNonRoutineSamples(HeaderNonRoutine model)
    {
//...some code
        PersonDetail pDetail = _db.ListPerson(_MePerson.First().Number.ToString());
        model.Name = pDetail.Name + " " + pDetail.Surname;
        model.Telephone = pDetail.PhoneWork;
        model.Division = pDetail.Division;
        model.RetainSample = 30;
        return PartialView(model);
    }

详情:

    public ActionResult _DetailNonRoutineSamples(List<DetailNonRoutine> model)
    {
        return PartialView(model);
    }

查看:

主要

@model LaboratorySampleRegistration.Models.SampleNonRoutine
@{
ViewBag.Title = "NonRoutineSamples";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<h4>Non-Routine Samples</h4>
<table>
    <tr>
        <td>
            @Html.Action("_HeaderNonRoutineSamples", "Home", Model.SampleHeader) XX1
        </td>
    </tr>
    <tr>
        <td>
            @Html.Action("_DetailNonRoutineSamples", "Home", Model.SampleDetail) XX2
        </td>
    </tr>
    <tr>
        <td>
            <div>
                @Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
                <div>
                    <span>
                        @Html.TextAreaFor(model => model.Comments, new { @class = "SampleRoutineComments" })
                        @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
                    </span>
                </div>
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <hr style="border-top:1px solid black !important;" />
            <p>
                <input id="SaveSend" name="SaveSend" type="submit" value="Send to Laboratory" class="btn btn-default" />
            </p>
        </td>
    </tr>
</table>

}

0 个答案:

没有答案