0

In my view file I current have the following code:

    @if ((Model.CustomerOrderTrackings != null) && (Model.CustomerOrderTrackings.Count > 0)) {
        for (int i = 0; i < Model.CustomerOrderTrackings.Count; i++) {
            @Html.EditorFor(m => m.CustomerOrderTrackings[i])
        }
    } else {
        @*The problem is here*@
        @Html.EditorFor(m => new CustomerOrderTracking())
    }

Then, my editor template looks like this:

@model Models.CustomerOrderTracking

<tr class="gridrow">
    <td class="carrierName">
        @Html.HiddenFor(m => m.Id, new { id = "" })
        @Html.HiddenFor(m => m.ShipperId, new { id = "", @class = "trackingShipperId" })
        @Html.DropDownListFor(m => m.CarrierName, ViewExtensions.ToFriendlySelectList<CustomerOrderTracking.CarrierNames>(Model.CarrierName), new { id = "" })
    </td>
    <td class="service">@Html.DropDownListFor(m => m.ShippingType, ViewExtensions.ToFriendlySelectList<CustomerOrderTracking.ShippingTypes>(Model.ShippingType), new { id = "" })</td>
    <td class="trackingNumber">@Html.TextBoxFor(m => m.ShippingTrackingNumber, new { @class = "trackingInput", id = "" }) <a href="" target="_blank" class="icon track"></a></td>
    <td class="shippingCost">
        @Html.TextBoxFor(m => m.ShippingCost, new { @class = "shippingInput", id = "" })
        <div onclick="Operations.Orders.DeleteTrackingRow(this)" class="icon delete deleteTracking" title="Delete this shipment?"></div>
    </td>
</tr>

What I'm trying to do is add a default row to this table in the event that there aren't currently any items attached to the object. The new instance of the object isn't working, since that results in the following error: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I could hand-code the inputs, but then my code is rather sloppy since I'd be using hand-coded inputs and then auto-generated inputs from the ASP.NET MVC extensions.

Is there a way that I can pass a default instance of the object to my editor template?

4

2 に答える 2

1

私の見方では、MVC フレームワークで目的を達成する方法は、コントローラーで要件を検出し、ビューに到達する前に必要に応じてモデルを調整することです。もう 1 つの答えは、99% 宗教的に適切な方法ではないということですが、自分の見解に論理を持たせることです (えー!)。

public ActionResult SomeAction()
{

  if (model.CustomerOrderTrackings == null 
      || model.CustomerOrderTrackings.Count > 0) 
  {
    // Do Something with model
  }

  this.View(model)
}
于 2012-05-14T16:00:52.410 に答える
0

私のビューファイルには、現在次のコードがあります。

@if ((Model.CustomerOrderTrackings != null) && (Model.CustomerOrderTrackings.Count > 0)) {
    for (int i = 0; i < Model.CustomerOrderTrackings.Count; i++) {
        @Html.EditorFor(m => m.CustomerOrderTrackings[i])
    }
} else {
    @*The problem is here*@
    @Html.EditorFor(m => new CustomerOrderTracking())
}

回避策は、モデルのインスタンスを Html EditorFor の外で宣言することです。例えば:

@if ((Model.CustomerOrderTrackings != null) && (Model.CustomerOrderTrackings.Count > 0)) {
    for (int i = 0; i < Model.CustomerOrderTrackings.Count; i++) {
        @Html.EditorFor(m => m.CustomerOrderTrackings[i])
    }
} else {
    @{ CustomerOrderTracking stubModel = new CustomerOrderTracking(); }
    @Html.EditorFor(m => stubModel)
}
于 2015-11-11T01:20:43.757 に答える