C# 使用条件运算符(IF..ELSE)时,MVC局部视图未渲染
我在我的C# 使用条件运算符(IF..ELSE)时,MVC局部视图未渲染,c#,asp.net-mvc,asp.net-mvc-partialview,C#,Asp.net Mvc,Asp.net Mvc Partialview,我在我的MVC部分视图中使用了一些IF..ELSE操作符,当我在其中使用IF..ELSE..operator时,我的部分视图没有渲染,有人知道我如何解决这个问题吗 或 我怎样才能以其他方式做到这一点 错误:“网络错误:500内部服务器错误- 编辑 我的模型不为空,CountryId也不为空 <a onclick="editTaxRates(@item.CountryTaxId)"><span class="fa fa-2x fa-edit" style="cursor:po
MVC部分视图
中使用了一些IF..ELSE
操作符,当我在其中使用IF..ELSE..operator
时,我的部分视图没有渲染,有人知道我如何解决这个问题吗或
我怎样才能以其他方式做到这一点 错误:“网络错误:500内部服务器错误- 编辑 我的模型不为空,CountryId也不为空
<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>
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>
控制器
public PartialViewResult EditTaxRates(int? CountryTaxId)
{
// conditional code
return PartialView(countryResult);
}
.csHtml(视图)
国家:
@TextBoxFor(m=>m.CountryName,new{@readonly=true})
税率:
@如果(Model.CountryId==1)
{
@TextBoxFor(m=>m.TaxRate,新的{@id=“C_firstCountry”})
}
其他的
{
@TextBoxFor(m=>m.TaxRate,新的{@id=“C_secondCountry”})
}
非常感谢您的宝贵反馈。与其编写IF语句,不如使用条件运算符?是它的MSDN页面 因此,我将按如下方式确定ID,而不是编写IF语句:
@Html.TextBoxFor(m => m.TaxRate, new { @id = Model.CountryID == 1 ? "C_firstCountry" : "C_secondCountry" })
因为我不确定如何加载局部视图,所以我只能建议这样做,如果局部视图是页面上的静态“控件/视图”
@{ Html.RenderPartial("_YourPartialView", Model); }
如果这个PartialView是动态加载的,您可以看看如何使用jquery和ajax。不用编写If语句,为什么不使用条件运算符呢?是它的MSDN页面 因此,我将按如下方式确定ID,而不是编写IF语句:
@Html.TextBoxFor(m => m.TaxRate, new { @id = Model.CountryID == 1 ? "C_firstCountry" : "C_secondCountry" })
因为我不确定如何加载局部视图,所以我只能建议这样做,如果局部视图是页面上的静态“控件/视图”
@{ Html.RenderPartial("_YourPartialView", Model); }
如果动态加载此PartialView,您可以看看如何使用jquery和ajax。smit
您的代码中几乎没有错误
在if条件下单独使用时,不能使用(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>
@Html.HiddenFor(row=>row.CountryTaxId)
国家:
@Html.TextBoxFor(row=>row.CountryName,新的{@placeholder=“更改国家名称”,@readonly=true})
税率:
@如果(Model.CountryId==1)
{
@Html.TextBoxFor(row=>row.TaxRate,新的{@placeholder=“更改税率”,@id=“TaxRate”})
}
smit
您的代码中几乎没有错误
在if条件下单独使用时,不能使用(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>
@Html.HiddenFor(row=>row.CountryTaxId)
国家:
@Html.TextBoxFor(row=>row.CountryName,新的{@placeholder=“更改国家名称”,@readonly=true})
税率:
@如果(Model.CountryId==1)
{
@Html.TextBoxFor(row=>row.TaxRate,新的{@placeholder=“更改税率”,@id=“TaxRate”})
}
我在if子句中有一个局部视图。在某些情况下,它无法实现
出于某种原因,将其从
@Html.Partial()
到
修好了
我现在没有时间也不想进一步研究这个问题,但下面可以解释原因:我在if子句中有一个局部观点。在某些情况下,它无法实现 出于某种原因,将其从
@Html.Partial()
到
修好了
我现在没有时间也不想进一步研究这个问题,但下面可能会解释原因:而不是
Model=>Model….
使用Model=>Model.
试试@if(Model.CountryId!=null&&Model.CountryId==1)试着把@/code>放在你的文本框前面。@KartikeyaKhosla,不使用model=>model
这是一个笑话吗?3次你被告知修复大写字母Mmodel
问题!不要使用Model=>Model….
使用Model=>Model。
试试@if(Model.CountryId!=null&&Model.CountryId==1)试着把@:
放在你的文本框前面。@KartikeyaKhosla,不使用Model=>Model
这是一个笑话吗?3次你被告知修复大写字母MModel
的问题!在你的例子中,你没有表现出这一点。如何加载局部视图?在示例中没有显示。如何加载您的局部视图?谢谢@Stuart的回答,但现在我问这个问题已经6年了@我知道,我只是提供我的解决方案给其他人看。谢谢@Stuart的回答,但现在我问这个问题已经6年了@我知道,我只是提供我的解决方案给别人看。