0

I have Partial View

Here is code

@model IEnumerable<SmartSolutions.Models.QuestionBlock>

@foreach (var item in Model) {
    <div>
        @Html.DisplayFor(modelItem => item.Question1)
    </div>       
}

Here code for controller

 public ActionResult Recording(int id)
    {
        /*var items = db.QuestionBlocks
            .Where(x => x.Interview_Id == id)
            .Select(x => x).ToList();*/
        ApplicationDbContext db = new ApplicationDbContext();
        return View();
    }

    public ActionResult QuestionBlock(int id) {

        ApplicationDbContext db = new ApplicationDbContext();
        var questionblocks = db.QuestionBlocks.Take(id);
        return PartialView(questionblocks);
    }

Here is code of View where I try to show PartialView

<div class="inner-div4" style="background: #ffffff">
<div class="counter-one">
    3/10
</div>
<div class="right-welcome-div2-borderless" style="background: #e5e5e5">
    <div class="timer-div-one" id="countdown" style="height: 20px; width: 20px;">

    </div>
    <div class="counter-div">
    </div>
    <p>@Html.Label("Show", htmlAttributes: new { @class = "showList", @style= "cursor:pointer;", data_rows = 1 })</p>
    <div id="questions">

    </div>

Here is AJAX call

<script>
$('.showList').click(function (e) {
    var rows_num = this.getAttribute("data-rows");
    $.ajax({
        type: "GET",
        url: "/interwier/QuestionBlock",
        data: { id: rows_num },
        sucess: function (data) {
            $("#questions").html(data);

        },
        error: function () {
            alert("Smth wrong in controller");
        }
    });
});

My problem in that < when I click <p>@Html.Label("Show", htmlAttributes: new { @class = "showList", @style= "cursor:pointer;", data_rows = 1 })</p> I see in Network console data, but not see on View.

6
  • What is the value of this? var questionblocks = db.QuestionBlocks.Take(id); Commented Mar 27, 2017 at 14:42
  • If I set breakpoint it's says id=1@mariocatch Commented Mar 27, 2017 at 14:45
  • What's the value of questionblocks ? Commented Mar 27, 2017 at 14:46
  • Did you check what is the count of questionblocks ? Commented Mar 27, 2017 at 14:46
  • Yes. One block@ChetanRanpariya Commented Mar 27, 2017 at 14:47

2 Answers 2

2

You have a typo in your AJAX code: sucess instead of success. Essentially, you have no success method at the moment, so nothing happens with the returned response.

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

5 Comments

Flash back to grade school English: "Double the 'C', double the 'S', and you will always have success". ;)
This is only one of the issues. This combined with the other answer from @Nayan Dhamsaniya is the actual solution.
@ChrisPratt The action needs to actually return which view to render, and then pass in the model as the second parameter.
@mariocatch: The data_rows member is an HTML attribute, not a route param. The OP accesses this attribute in the JS and then passes id to the action, as required.
@ChrisPratt I'm not referring to data_rows. I'm saying calling return PartialView(questionblocks) may not have been correct as we don't know what the value of questionblocks actually was. It could've been an int for all we know. Hard to know if that actually mapped to a partial view in his project.
1

I think change parameter name data_rows to id. so please try it.

And simple changes in your method.

public ActionResult QuestionBlock(int id) {

    ApplicationDbContext db = new ApplicationDbContext();
    var questionblocks = db.QuestionBlocks.Take(id);
    return PartialView("Name_Of_PartialView",questionblocks);
}

Comments

Your Answer

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