2013-03-13 1 views
0

POnumber가 null 인 경우 일부 텍스트를 반환하는 다음 뷰가 있습니다. if (Model.Invoice.PONumber == null) 대신 LineNumber, Description, UnitOfMeasure, QtyOrdered 필드를 검사하는 검사 메커니즘 (여러 if 문)이 ​​필요하다고 생각합니다. null이면 N/A 또는 빈 공간으로 바뀌지 만 사용자가 사용할 수있는 나머지 정보를 볼 수 있습니다. 수혈이 있습니까? 나는 MVC에 익숙하지 않으며 어떤 도움도받을 가치가 없다. MVC3 ASP null 값을 뷰의 빈 공간으로 바꿉니다.

, 당신의 시간과 도움을 사전에 바비 감사

<div class="contentWrapper2"> 
    <div class="content2"> 
     <div class="clr lfl w100"> 
      <h1>Invoice Detail</h1> 
      <div class="return-btn"> 
       <a class="btn btnStyleC btn-back-invoice" href="@Url.Action("InvoiceHistory", "Account")"> 
        Back to Invoice List</a> 
      </div> 
     </div> 
     @if (Model.ErpError.Length > 0) 
     { 
      <div class="clr lfl w100 error"> 
       @Html.Raw(Model.ErpError) 
      </div> 
     } 
     else 
     { 
      if(Model.Invoice.PONumber == null) 
      { 
       <div class="lfl w100 clr messaging"> 
        <p>No information available at the moment for current invoice. 
         Please call our sales department for further assistance. 
        </p> 
       </div> 
      } 
      else 
      { 
       <div class="clr lfl w100"> 
        <div class="order-number-date"> 
         <table> 
          <tr> 
           <th class="col-1"> 
            <h3>Invoice #:</h3> 
           </th> 
           <td class="col-2"> 
            <h3>@Model.Invoice.InvoiceNumber</h3> 
           </td> 
          </tr> 
          <tr> 
           <th class="col-1"> 
            <h3>Invoice Date:</h3> 
           </th> 
           <td class="col-2"> 
            <h3>@Model.Invoice.InvoiceDate.ToShortDateString()</h3> 
           </td> 
          </tr> 
         </table> 
        </div> 
        <div class="order-number-date"> 
         <table> 
          <tr> 
           <th class="col-1"> 
            <h3>Order #:</h3> 
           </th> 
           <td class="col-2"> 
            <h3>@Model.Invoice.OrderNumber</h3> 
           </td> 
          </tr> 
          <tr> 
           <th class="col-1"> 
            <h3>PO #:</h3> 
           </th> 
           <td class="col-2"> 
            <h3>@Model.Invoice.PONumber</h3> 
           </td> 
          </tr> 
          <tr> 
           <th class="col-1"> 
            <h3>Due Date:</h3> 
           </th> 
           <td class="col-2"> 
            <h3>@Model.Invoice.DueDate.ToShortDateString()</h3> 
           </td> 
          </tr> 
         </table> 
        </div> 
       </div> 
      <div class="clr lfl w100"> 
       <div class="bill-ship"> 
        <table> 
         <tr> 
          <th> 
           <h4>Billing Information</h4> 
          </th> 
         </tr> 
         <tr> 
          <td>@Model.Invoice.BTDisplayName 
          </td> 
         </tr> 
         <tr> 
          <td> 
           <@Html.Raw(Model.Invoice.BTAddress1) 
          </td> 
         </tr> 
         @if (!string.IsNullOrEmpty(Model.Invoice.BTAddress2)) 
         { 
          <tr> 
           <td>@Html.Raw(Model.Invoice.BTAddress2) 
           </td> 
          </tr> 
         } 
         <tr> 
          <td>@Html.CityCommaStateZip(Model.Invoice.BTCity, Model.Invoice.BTState, Model.Invoice.BTZip)</td> 
         </tr> 
         <tr> 
          <td>@Model.Invoice.BTCountry 
          </td> 
         </tr> 
         <tr> 
          <td>@Model.Invoice.BTPhone1</td> 
         </tr> 
         <tr> 
          <td>@Model.Invoice.BTEmail 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      if (Model.Invoice.InvoiceLines.Count > 0) 
      { 
       <div class="clr lfl w100 line-item-detail"> 
        <table class="info-tbl"> 
         <tr> 
          <th class="vid-item">Item #</th> 
          <th class="vid-desc">Description</th> 
          <th class="vid-um"> 
           U/M 
          </th> 
          <th class="vid-qty"> 
           Qty 
          </th> 
          <th class="vid-ship"> 
           Ship Date 
          </th> 
          @if (Model.ShowPackslip) 
          { 
           <th class="vid-pack">Pack Slip</th> 
          } 
          <th class="vid-unit">Unit Price</th> 
          <th class="vid-ext">Ext Price</th> 
         </tr> 
         @foreach (var invoiceLine in Model.Invoice.InvoiceLines) 
         { 
          <tr> 
           <td class="vid-line">@invoiceLine.LineNumber</td> 
           <td class="vid-desc">@invoiceLine.Description</td> 
           <td class="vid-um">@invoiceLine.UnitOfMeasure</td> 
           <td class="vid-qty">@invoiceLine.QtyOrdered</td> 
           <td class="vid-ship"> 
           @if (invoiceLine.ShipDate.ToShortDateString() == "1/1/0001") 
           { 
           } 
           else 
           { 
            @invoiceLine.ShipDate.ToShortDateString() 
           } 
           </td> 
           @if (Model.ShowPackslip) 
           { 
            <td class="vid-pack"> 
            <a href="@Url.RouteUrl(new { controller = "Account", action = "ShipmentDetail", PackSlipNum = invoiceLine.PackSlip })">@invoiceLine.PackSlip</a> 
            </td> 
           } 
           <td class="vid-unit">@invoiceLine.UnitPrice.ToCurrency() 
           </td> 
           <td class="vid-ext">@invoiceLine.ExtendedPrice.ToCurrency() 
           </td> 
          </tr> 
         } 
        </table> 
       </div> 
      } 
      <div class="clr lfl w100"> 
       <table class="tbl-total"> 
        <tr class="subtotal"> 
         <th class="col-1">Subtotal</th> 
         <td class="col-2">@Model.Invoice.OrderSubTotal.ToCurrency() 
         </td> 
        </tr> 
        @if (Model.Invoice.DollarOffOrder > 0) 
        { 
         <tr> 
          <th class="col-1">Order Discount</th> 
          <td class="col-2">@Model.Invoice.DollarOffOrder.ToCurrency()</td> 
         </tr> 
        } 
        @if (Model.Invoice.ShippingAndHandling > 0) 
        { 
         <tr> 
          <th class="col-1">Shipping</th> 
          <td class="col-2">@Model.Invoice.ShippingAndHandling.ToCurrency() 
          </td> 
         </tr> 
        } 
        @if (Model.Invoice.MiscCharges > 0) 
        { 
         <tr> 
          <th class="col-1">Misc. Charges</th> 
          <td class="col-2">@Model.Invoice.MiscCharges.ToCurrency()</td> 
         </tr> 
        } 
        <tr> 
         <th class="col-1">Sales Tax</th> 
         <td class="col-2">@Model.Invoice.TotalTax.ToCurrency()</td> 
        </tr> 
        <tr> 
         <th class="col-1">Invoice Total</th> 
         <td class="col-2">@Model.Invoice.InvoiceTotal.ToCurrency()</td> 
        </tr> 
       </table> 
      </div> 
      <div class="clr lfl w100"> 
     <a class="btn btnStyleB btn-print" href="javascript:window.print();">Print</a> 
     </div> 
    } 
} 
</div> 
</div> 

답변

0

당신이 할 수처럼 예 "nullcheck.cshtml"을 촉구 템플릿 생성 :

@if (ViewBag.ValueToCheck == null) { 
    <div class="lfl w100 clr messaging"> 
    <p> 
     No information available at the moment for @(ViewBag.Field). 
     Please call our sales department for further assistance. 
    </p> 
    </div> 
} 
else { 
    @Html.Partial(ViewBag.TargetTemplate, Model) 
} 

는 그런 다음에서 호출을하여 기본보기 :

@{ 
    ViewBag.TargetTemplate = "okModel"; 
    ViewBag.Field = "P.O.Number"; 
    ViewBag.ValueToCheck = Model.Invoice.PONumber; 
    Html.RenderPartial("nullCheck", Model, ViewBag); 
} 

okModel.cshtml은 템플릿의 일부 여야합니다. 값은 ... 나 자신 나는 이것을 테스트하지 않았습니다

널 (null)이 아니라 일을 갈 경우 잘못된 XD에게

건배를 ... 당신에게 몇 가지 아이디어를주고 저에게 연락한다!

0

이것은 컨트롤러에서 처리해야하는 것처럼 보입니다.

@Model.Invoice.PONumber ?? "NA" 
:

public ActionResult YourControllerAction() 
{ 
    var myViewModel = SomeService.GetMyViewModel(); 
    if (myViewModel.Invoice.PONumber == null) 
    { 
     myViewModel.Invoice.PONumber = "N/A"; 
    } 

    //etc 
} 

이 당신이 단순히 null coalescing operator과 같이를 사용할 수있는보기에서보기 명확하게 (내 개인적인 취향) 그러나

관련 문제