2016-03-08 80 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

你没有在你的例子中显示。你如何加载你的部分视图? –