Reputation:
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?
Upvotes: 1
Views: 565
Reputation:
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
Upvotes: 1