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


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


<table class="table-condensed table-hover table-responsive table-bordered two small header" id="adjustmentTable">


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

        url: "/TOSAdjustment/Index",
        type: "POST",
        data: {
            ssoSelected: $(sso).text(),
            monthSelected: $('input[name=monthOptions]:checked').val()
    .done(function (res) {
        $("#msg").html("Response from search received");
    }).fail(function (x, a, 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