1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is working but I use foreach loop in _MasrafTurleriGetir and when I run this append code, I get "unterminated string literal" error which strings are coming from _MasrafTurleriGetir.
I added <text>\</text> and "unterminated string literal" problem solved. Thank you btw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.