【问题标题】:MVC Partial view is not rendering when use Conditional operator (IF..ELSE)使用条件运算符 (IF..ELSE) 时,MVC 部分视图不呈现
【发布时间】:2016-03-08 08:56:45
【问题描述】:

我在我的MVC Partial View 中使用IF..ELSE 运算符进行了一些工作,当我在其中使用IF..Else..Operator 时,我的部分视图没有呈现,有人知道我该如何解决这个问题吗?



我怎么能以其他方式做到这一点?

错误:“NetworkError:500 内部服务器错误 - http://localhost:50101/TaxRates/EditTaxRates?CountryTaxId=12

编辑

我的模型不为空,CountryId 也不为空

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>

感谢您提供宝贵的反馈。

【问题讨论】:

  • 而不是Model =&gt;Model.... 使用model =&gt;model.
  • 试试@if (Model.CountryId != null && Model.CountryId == 1)
  • 尝试将 @: 放在 TextBoxFor 之前。
  • @KartikeyaKhosla,不适用于model =&gt; model
  • 这是在开玩笑吗 - 3 次你被告知要修复大写 M Model 问题!

标签: c# asp.net-mvc asp.net-mvc-partialview


【解决方案1】:

我对 if 子句有部分看法。在某些情况下,它无法呈现。

由于某种原因,将其更改为

@Html.Partial()

@Html.RenderPartial()

修复它。

我现在没有时间也没有兴趣进一步研究这个问题,但以下内容可以解释原因:Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

【讨论】:

  • 感谢@Stuart 的回答,但现在我问这个问题已经 6 年了……!!
  • @SmitPatel 我知道,我只是提供我的解决方案供其他人查看。
【解决方案2】:

提交 您的代码几乎没有错误 当您在 if 条件 @if(Model.CountryId == 1) 中单独使用 @if(Model.CountryId == 1) 时,您不能使用 @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>

【讨论】:

    【解决方案3】:

    您为什么不使用条件运算符而不是编写 IF 语句? Here 是它的 MSDN 页面。

    因此,我将确定 ID 如下,而不是编写您的 IF 语句:

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

    由于我不确定您如何加载局部视图,因此我只能建议您这样做,前提是您的局部视图是页面上的静态“控件/视图”。

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

    如果这个 PartialView 是动态加载的,你可以看看使用 jquery 和 ajax。

    【讨论】:

    • 您没有在示例中显示。你如何加载你的局部视图?
    猜你喜欢
    • 1970-01-01
    • 2017-05-06
    • 2014-08-11
    • 2019-10-20
    • 2023-03-03
    • 1970-01-01
    • 2012-04-13
    • 2013-05-28
    • 2016-07-21
    相关资源
    最近更新 更多