2016-03-08 79 views
0

我有一些IF..ELSE運算符在我的MVC Partial View,我的部分視圖不是呈現當我使用IF..Else..Operator在裏面,任何人都可以知道我該如何解決這個?

OR

我怎麼能做到這一點在其他的方式?MVC部分視圖不使用時呈現使用條件運算符(IF..ELSE)

錯誤: 「NetworkError:500內部服務器錯誤 - http://localhost:50101/TaxRates/EditTaxRates?CountryTaxId=12

EDIT

我的模型不爲空,也CountryId IS NOT NULL

HTML

<a onclick="editTaxRates(@item.CountryTaxId)"><span class="fa fa-2x fa-edit" style="cursor:pointer; color:black" title='Click to Edit Country'></span></a> 
<form id="readIODetail" style="z-index:100"> 
     <div id="divAddCountryRecord" class="modal hide fade " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style="width: 700px; left: 46% !important;"> 
     </div> 
</form> 

jQuery的

function editTaxRates(CountryTaxId) { 
     var selectedCountryType = $("#selectedCountryType").val(); 
     $.ajax({ 
      type: 'POST', 
      url: '/TaxRates/EditTaxRates?CountryTaxId=' + CountryTaxId, 
      success: function (response) { 
       $("#divAddCountryRecord").empty(); 
       $("#divAddCountryRecord").append(response); 
       $('#divAddCountryRecord').modal('show'); 
      } 
     }) 
    } 

控制器

public PartialViewResult EditTaxRates(int? CountryTaxId) 
    { 
     // conditional code 
     return PartialView(countryResult); 
    } 

.csHtml(查看)

<div class="modal-body" style="padding-left:10px;overflow-y:auto;"> 
    <div class="row-fluid container"> 
     <div class="span4"> 
     </div> 
     <div class="row-fluid span12"> 
      <div class="span3"> 
       <p><strong>Country: </strong></p> 
      </div> 
      <div class="span5"> 
       @Html.TextBoxFor(m => m.CountryName, new { @readonly = true }) 
      </div> 
     </div> 
     <div class="row-fluid span12"> 
      <div class="span3"> 
       <p><strong>Tax-Rate: </strong></p> 
      </div> 
      <div class="span5"> 
       @if (Model.CountryId == 1) 
       { 
        @Html.TextBoxFor(m => m.TaxRate, new { @id = "C_firstCountry" }) 
       } 
       else 
       { 
        @Html.TextBoxFor(m => m.TaxRate, new { @id = "C_secondCountry" }) 
       } 
      </div> 
     </div> 
    </div> 
</div> 

您寶貴的意見表示讚賞。

+0

代替'Model => Model ....'使用'model => model.' –

+0

嘗試@if(Model.CountryId!= null && Model.CountryId == 1) –

+0

嘗試將'@:'放在你的TextBoxFor's。 –

回答

1

SMIT 有在你的代碼 很少的錯誤,當你在你的,如果條件seperately使用它,你不能使用(Model => Model.CountryName ....)@if(Model.CountryId == 1)

所以,請更換您的代碼下面的代碼&它會工作

<div class="modal-body" style="padding-left:10px;overflow-y:auto;"> 
     <div class="row-fluid container"> 
      @Html.HiddenFor(row => row.CountryTaxId) 
      <div class="span4"> 
      </div> 
      <div class="row-fluid span12"> 
       <div class="span3"> 
        <p><strong>Country: </strong></p> 
       </div> 
       <div class="span5"> 
        @Html.TextBoxFor(row => row.CountryName, new { @placeholder = "Change the Country Name", @readonly = true }) 
       </div> 
      </div> 
      <div class="row-fluid span12"> 
       <div class="span3"> 
        <p><strong>Tax-Rate: </strong></p> 
       </div> 
       <div class="span5"> 
        @if (Model.CountryId == 1) 
        { 
         @Html.TextBoxFor(row => row.TaxRate, new { @placeholder = "Change the Tax-Rate", @id = "TaxRate" }) 
        } 
       </div> 
      </div> 
     </div> 
    </div> 
+0

感謝您的回答,您的代碼正在工作 –

0

而不是編寫該IF語句,爲什麼不使用條件運算符? Here是它的MSDN頁面。

所以,我決定,而不是寫你的IF語句的ID如下,:

@Html.TextBoxFor(m => m.TaxRate, new { @id = Model.CountryID == 1 ? "C_firstCountry" : "C_secondCountry" }) 

因爲我不知道你如何加載你的部分觀點,我只能建議這一點,如果局部視圖是您網頁上的靜態「控制/查看」。

@{ Html.RenderPartial("_YourPartialView", Model); } 

如果這個PartialView被動態加載,你可以看看使用jQuery和ajax。

+0

與多個'If Else'條件一起使用。 –

+1

你沒有在你的例子中顯示。你如何加載你的部分視圖? –