0

I have some work with IF..ELSE operator in my MVC Partial View, My partial view is not rendering when i use IF..Else..Operator in it, Can anybody know how can i solve this?

OR

How can i do this in other way?

ERROR : "NetworkError: 500 Internal Server Error - http://localhost:50101/TaxRates/EditTaxRates?CountryTaxId=12"

EDIT

My model is not null, also the 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');
            }
        })
    }

Controller

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

.csHtml (View)

<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>

Your valuable feedback is appreciated.

Smit Patel
  • 2,992
  • 1
  • 26
  • 44
  • instead of `Model =>Model....` use `model =>model.` – Kartikeya Khosla Mar 08 '16 at 08:58
  • try @if (Model.CountryId != null && Model.CountryId == 1) – Paul Zahra Mar 08 '16 at 08:58
  • try putting `@: ` before your TextBoxFor's. – Erik Philips Mar 08 '16 at 08:59
  • @KartikeyaKhosla, Not working with `model => model` – Smit Patel Mar 08 '16 at 09:00
  • What do you mean its not rendering? What is happening. And what is the point of your `if/else` block (all it does is change the `id` attribute) –  Mar 08 '16 at 09:02
  • @PaulZahra, Countryid is int, So No need to check with `NULL` – Smit Patel Mar 08 '16 at 09:03
  • @StephenMuecke, When i remove this condition the code will work . – Smit Patel Mar 08 '16 at 09:03
  • Check if `Model` is `null` – Mike Debela Mar 08 '16 at 09:04
  • But what is not working if you add it? –  Mar 08 '16 at 09:05
  • Partial view is not rendering Or simple terms it will not show the Popup window – Smit Patel Mar 08 '16 at 09:07
  • Then what errors are you getting. Debug your code. –  Mar 08 '16 at 09:12
  • And you need to change all your expressions to `@Html.TextBoxFor(m => m.CountryName)` etc (not capital M `Model`) since you have already used `Model` in your if block. And debugging you code would make if obvious that it was throwing an exception) –  Mar 08 '16 at 09:17
  • @StephenMuecke, Not working with your change. – Smit Patel Mar 08 '16 at 09:20
  • What is not working? If you do not make the changes I said, your would be throwing an exception. –  Mar 08 '16 at 09:22
  • Test TaxRate for nulls... also change Model => Model.TaxRate to m => m.TaxRate – Paul Zahra Mar 08 '16 at 09:23
  • @SmitPatel Simple elimination... remove both lines of Html.TextBoxFor(m => m.TaxRate, new { id = .... does it render now? – Paul Zahra Mar 08 '16 at 09:25
  • Now your edit states a `500 Internal Server Error` - which means your controller method is throwing and exception. And I assume is the result of an ajax call. Show the relevant code! –  Mar 08 '16 at 09:29
  • @PaulZahra No it's not – Smit Patel Mar 08 '16 at 09:31
  • Guys check the Edit Question now – Smit Patel Mar 08 '16 at 09:37
  • Which bit about _your controller method is throwing an exception_ do you not understand. Debug your code! (we can't guess what it is) –  Mar 08 '16 at 09:45
  • Already checked multiple times but why it's throwing an Exception as it is working the same when i remove `If else` condition, Can you please say me why this error occurred as it is not occurring with a removal of 'if else' condition. – Smit Patel Mar 08 '16 at 09:48
  • We cannot guess the code in your controller when you do not show it. And one of the exceptions would have been because you are using capital M `Model` in your expressions (and if you inspect the file, you would be seeing lots of red underlining). but as I noted earlier, what is the point of your `if/else` block? –  Mar 08 '16 at 09:53
  • @SmitPatel Ideally you should not be doing any logic in the view... you should perform your logic in the controller... and put the result in a viewmodel... then your view just dumps out whats in the model... the view is only for presentation logic at most. – Paul Zahra Mar 08 '16 at 10:07
  • @PaulZahra, adding an `id` attribute is presentation logic only (its only possible purpose is to use as a javascript/jquery selector) –  Mar 08 '16 at 10:13
  • 1
    Is this a joke - 3 time you were told to fix the capital M `Model` issue! –  Mar 08 '16 at 11:20

3 Answers3

2

I had a partial view in an if clause. In some circumstances it failed to render.

For some reason, changing it from

@Html.Partial()

to

@Html.RenderPartial()

fixed it.

I haven't the time nor inclination to look further into this now, but the following may explain why: Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

Stuart Aitken
  • 949
  • 1
  • 13
  • 30
1

smit there is very little mistake in your code you can not use (Model => Model.CountryName ....) when you are using it seperately in your if condition @if(Model.CountryId == 1)

So please replace your code to below code & it will work

<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

Instead of writing that IF statement, why don't you use the conditional operator? Here is the MSDN page for it.

So, I would determine the ID as follow, instead of writing your IF statement:

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

Since I am not sure how you load your partial view, I can only suggest this, if you partial view is a static "control/view" on your page.

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

If this PartialView gets loaded dynamically, you can have a look at using jquery and ajax.

monstertjie_za
  • 7,277
  • 8
  • 42
  • 73