Home How to build Razor syntax in Controller and render it in view
Reply: 0

How to build Razor syntax in Controller and render it in view

user2673
1#
user2673 Published in April 22, 2018, 4:56 am

I have an Ajax request that builds a table in my controller. I'm attempting to create a dropdown list using razor @Html Helpers of @Html.DropDownListFor in the controller, then return it to my view. The view renders the @Html.DropDownListFor as raw syntax. It seems I lose scope of razor completely when it gets built in the controller then passed to my view.

Is there a way to build/construct razor syntax server side and have it render successfully in a view?

Controller

public ActionResult Index(string siteSelected, string pcmSelected, string busGrpSelected, string monthSelected, string ssoSelected){

var associateResults = _appResult.TOSAdjustmentResult(siteSelected, busGrpSelected, pcmSelected, b.getMonth(monthSelected, true));

foreach (var item in associateAdjusmentResults.view_tos_with_adjustment_final)
                {

                    t1 += "<td>" + item.DIRECTION + "</td>";
                    t1 += "<td>" + item.CALLS + "</td>";
                    t1 += "<td>" + item.TALK + "</td>";
                    t1 += "<td>" + item.HOLD + "</td>";
                    t1 += "<td>@Html.DropDownListFor(model => model.site, new SelectList(Model.site), '--Site--', new { @id = 'siteId' })</td>";
                    t1 += "</tr>";
                }

                return Content(t1);
}

View

<table class="table-condensed table-hover table-responsive table-bordered two small header" id="adjustmentTable">
    <tr>
        <td>
            Direction
        </td>
        <td>
            Calls
        </td>
        <td>
            Talk
        </td>
        <td>
            Hold
        </td>
        <td>
            DropDownList
        </td>
    </tr>
</table>

Jquery/Ajax

$(".clickableRow").on("click", ".clickableRow", function () {
    //Getting SSO from search table
    var sso = this.cells[1];

    $("#adjustmentTable").find("tr:gt(0)").remove();
    $.ajax({
        url: "/TOSAdjustment/Index",
        type: "POST",
        data: {
            ssoSelected: $(sso).text(),
            monthSelected: $('input[name=monthOptions]:checked').val()
        }
    })
    .done(function (res) {
        $("#msg").html("Response from search received");
        $("#adjustmentTable").append(res);
    }).fail(function (x, a, e) {
        alert(e);
    });
});

Table Result

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298569 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO