OwnurD
OwnurD

Reputation: 195

How to append Html.Action to view by using javascript

I am trying to add new row by clicking add new button. Row contains below

<div class="col-sm-12" id="totalRow" cnt="1">
    <div class="col-sm-6" style="margin-top: 10px; float: left; padding-left: 0;">
        <label for="reg_input">Masrafın Türü</label>
        <select id="s2_basic_masrafbutce" name="masrafTuruId">
             <option value=""></option>
             <optgroup label="Masraf Türleri">
                @Html.Action("_MasrafTurleriniGetir", "Ortak", new { ID = 0 })
             </optgroup>
         </select>
    </div>
    <div class="col-sm-3" style="margin-top: 10px; float: right; padding-right: 0px;">
        <label for="reg_input">Masrafın Tutarı</label>
        <input type="text" id="@Html.IdFor(x => x.tutar)" class="form-control" name="tutar">
    </div>
    <div class="col-sm-3" style="padding-right: 0; margin-top: 10px; float: right">
        <label for="reg_input">Fiş No</label>
        <input type="text" id="@Html.IdFor(x => x.fis_no)" class="form-control" name="fis_no">
    </div>
    <div class="col-sm-6" style="margin-top: 20px; float: left; padding-left: 0">
        <label for="reg_input">Açıklama</label>
        <textarea name="MasrafAciklama" class="form-control" style="resize: none;">                      </textarea>
    </div>
</div>

How can I append

@Html.Action("_MasrafTurleriniGetir", "Ortak", new { ID = 0 }) 

into div by using javascript ?

I tried

$("#myDiv").append('<%=Html.Action("_MasrafTurleriniGetir", "Ortak", new { ID = 0 })');

but it's not working.

Upvotes: 1

Views: 758

Answers (1)

twoflower
twoflower

Reputation: 6830

You are apparently using the Razor view engine, so this should work

$("#myDiv").append('@Html.Action("_MasrafTurleriniGetir", "Ortak", new { ID = 0 })');

Also make sure the javascript is part of a cshtml file, not an external JavaScript file.

Upvotes: 2

Related Questions