1

I have div where I need to display partial View via AJAX call to Controller

I have Table

Here is table

CREATE TABLE [dbo].[QuestionBlocks] (
[Block_ID]     INT            IDENTITY (1, 1) NOT NULL,
[Question1]    NVARCHAR (MAX) NULL,
[Question2]    NVARCHAR (MAX) NULL,
[Question3]    NVARCHAR (MAX) NULL,
[Question4]    NVARCHAR (MAX) NULL,
[Question5]    NVARCHAR (MAX) NULL,
[Question6]    NVARCHAR (MAX) NULL,
[Question7]    NVARCHAR (MAX) NULL,
[Question8]    NVARCHAR (MAX) NULL,
[Question9]    NVARCHAR (MAX) NULL,
[Question10]   NVARCHAR (MAX) NULL,
[Interview_Id] INT            NULL,
[QuestionId]   INT            NULL,
PRIMARY KEY CLUSTERED ([Block_ID] ASC),
CONSTRAINT [FK_QuestionBlocks_ToTable] FOREIGN KEY ([Interview_Id]) REFERENCES [dbo].[Interviews] ([Interview_Id]) ON DELETE CASCADE);

I need to display row where Interview_Id will be id

Id is id from url. My url is like *****.***/Interwier/Recording/id

I wrote Action methods for PartialView

Here is code.

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

And here is AJAX call

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "/interwier/QuestionBlock",
        data: { id: rows_num = 1 },
        success: function (data) {
            $("#questions").html(data);
        },
        error: function () {
            alert("Smth wrong in controller");
        }
    });
});

Here is PArtialView. But I think it is useless in this question

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

All that I need is get id from url, how I can do this?

5
  • The way I fixed this in one of my problem was to set Id to a session variable or a global variable in "public ActionResult Recording(int id)" method and use that variable "QuestionBlock" Commented Mar 28, 2017 at 7:52
  • I found way to get id from url via JS. It's looks like this var url = window.location.pathname; var id = url.substring(url.lastIndexOf('/' + 1));. Will now check , if it works as I expected. @devil_coder Commented Mar 28, 2017 at 7:54
  • Good Job @E.S Thanks Commented Mar 28, 2017 at 7:58
  • Look, my code gets all after localhost:5654. Will now make code to get only id @devil_coder Commented Mar 28, 2017 at 8:01
  • var full_url = document.URL; // Get current url var url_array = full_url.split('/') // Split the string into an array with / as separator var last_segment = url_array[url_array.length - 1]; // Get the last part of the array (-1) alert(last_segment); This code is works perfectly. @devil_coder Commented Mar 28, 2017 at 8:06

1 Answer 1

1

So I found answer

Here is code for getting id from url

var full_url = document.URL; // Get current url var url_array = full_url.split('/') // Split the string into an array with / as separator var last_segment = url_array[url_array.length - 1]; // Get the last part of the array (-1) alert(last_segment);

And Controller is left as-is

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

Comments

Your Answer

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