无法获取(列表)要从部分视图绑定的对象属性

时间:2014-06-20 07:48:27

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

嗨,我是MVC的新手,对于我的第一个项目,我一直坚持将后期数据绑定到视图模型。我在MVC方面经验丰富的同事已经放弃了尝试解决它的问题,现在它已经上网了。我真的相信这应该不会很难,我搞砸了一些惯例或其他东西。我知道有数百个关于数据绑定的问题,但每个问题都是微妙的不同。

在我的示例中,ColourSelectionDTO对象拒绝绑定。当我获得回发到控制器时,View Model具有除ColourSelections之外的所有内容。实际上,ColourSelections包含5个ColourSelectionDTO实例,但在每个实例中,属性都为null。

结束了我的问题(我需要ColourSelectionDTO的属性来绑定),我现在要列出代码的缩减版本,我已经包含名称空间,以防我错过了那里的约定,但不是使用语句,请假设该方是正确的,因为代码编译并运行。谢谢你们。

命名空间MyProject.Model.DTO中的ColourSelectionDTO.cs

/// <summary>
/// intended as a view model for the partial view with the same name
/// </summary>
public class ColourSelectionDTO
{
    public string SelectedColourStr;
    public string Part;
}

在命名空间MyProject.Model.DTO.Ordering中的SubmitOrderInputDTO.cs

public class SubmitOrderInputDTO
{
    public long JobNum { get; set; }

    public List<ColourSelectionDTO> ColourSelections { get; set; }

    //other properties that seem to bind fine
    ....

}

MyProject.Web.ViewModels.JobNextActionSubmitOrder中的SubmitOrderVM.cs

public class SubmitOrderVM
{
    public SubmitOrderDTO Data { get; set; }
    public SubmitOrderInputDTO Inputs { get; set; }

}

MyProject.Web.Views.JobNextActionSubmitOrder中的SubmitOrder.cshtml

@model SubmitOrderVM
@{
    ViewBag.Title = "Next Action - Submit Order";
    Layout = "~/Views/Job/_JobDetailsLayout.cshtml";
}

<div class="job-details-content">
@using (Html.BeginForm("SubmitOrder", "JobNextActionSubmitOrder", FormMethod.Post, new { id = "job-next-action-form" }))
{
    @Html.HiddenFor(m => m.Inputs.JobNum)
    @Html.ValidationSummary(false)

    <!--goes on for bunch of form stuff that I'm happy works ok-->

        for (int i = 0; i < Model.Data.AvailableColours.Keys.Count; i++)
        {
            //the index range for the loop does exactly match the index range of Model.Data.AvailableColours
            <div class="row">
                @Html.EditorFor(m => m.Inputs.ColourSelections[i])
            </div>
        }

    <!--more stuff that works including the submit button-->

MyProject.Web.Views.Shared.EditorTemplates中的ColourSelectionDTO.cshtml

@Html.HiddenFor(m => m.Part) <!--value of part expected to be set by business layer-->
@Html.TextBoxFor(m => m.SelectedColourStr)

JobNextActionSubmitOrderController(简化为仅显示相关位)

public ActionResult Index(long jobNum)
{
    SubmitOrderVM submitVM = new SubmitOrderVM();
    submitVM.Data = _jobSubmitOrderBL.GetOrderForSubmission(jobNum);
    //below call will initialise the ColourSelections list and each ColourSelectionDTO
    //within, and sets the Part property but not the SelectedColourStr property
    submitVM.Inputs = _jobSubmitOrderBL.GetDefaultInputsForSubmitOrder(submitVM.Data);
    return View("SubmitOrder", submitVM);
}

    [HttpPost]
    public ActionResult SubmitOrder(SubmitOrderVM vm)
    {
        //this is where the vm.Inputs.ColourSelections[0..n] come back initalised
        //but .Part and .SelectedColourStr are null
        var result = _jobSubmitOrderBL.SubmitJobOrderAndProcessAllPossibleActions(vm.Inputs);
        return RedirectToAction("SubmitOrderResults");
    }

ColourSelectionDTO.cshtml生成的html是:

<input id="Inputs_ColourSelections_0__Part" name="Inputs.ColourSelections[0].Part" type="hidden" value="Walls" /> <!--value of part expected to be set by business layer-->
<input id="Inputs_ColourSelections_0__SelectedColourStr" name="Inputs.ColourSelections[0].SelectedColourStr" type="text" value="" />

帖子数据看起来像这样

Inputs.JobNum:141085
Inputs.RequestedDeliveryDate:06/07/2014
Inputs.ColourSelections[0].Part:Walls
Inputs.ColourSelections[0].SelectedColourStr:testcol
Inputs.ColourSelections[1].Part:Roof
Inputs.ColourSelections[1].SelectedColourStr:blue
Inputs.ColourSelections[2].Part:Gutter
Inputs.ColourSelections[2].SelectedColourStr:orange
Inputs.ColourSelections[3].Part:CornerFlashing
Inputs.ColourSelections[3].SelectedColourStr:White
Inputs.ColourSelections[4].Part:RollerDoors
Inputs.ColourSelections[4].SelectedColourStr:Zinc
Inputs.IsAdditionalItemsChecked:true
Inputs.IsAdditionalItemsChecked:false
Inputs.IsBOMFinalised:false
Inputs.ClientOrderDocsSelected:DeliveryLetter
Inputs.ClientOrderDocsSendOptionsSelected:View
Inputs.IsSendGreensheet:false

1 个答案:

答案 0 :(得分:0)

MVC不会绑定模型中的变量,它们需要是属性。在我的例子中,您可以在ColourSelectionDTO的第一个代码snipet中看到错误。只是一个错字,我离开了{得到;组;部分,但是要表明一个小错误会让你长时间挠头。