Tengo div donde necesito mostrar una vista parcial a través de una llamada AJAX al controlador
tengo mesa
aquí está la mesa
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); Necesito mostrar la fila donde se id Interview_Id
Id es id de url. Mi URL es como *****.***/Interwier/Recording/id
Escribí métodos de acción para PartialView
Aquí está el código.
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); }Y aquí está la llamada AJAX
$(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"); } }); });Aquí está PArtialView. Pero creo que es inútil en esta pregunta.
@model IEnumerable<SmartSolutions.Models.QuestionBlock> @foreach (var item in Model) { <div> @Html.DisplayFor(modelItem => item.Question1) </div> }Todo lo que necesito es obtener la identificación de la URL, ¿cómo puedo hacer esto?
Entonces encontré la respuesta
Aquí está el código para obtener la identificación de la 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);Y el controlador se deja como está