通过AJAX选择表中的行并显示它(ASP.NET MVC)

时间:2017-03-28 07:45:38

标签: jquery asp.net ajax asp.net-mvc

我有div,我需要通过AJAX调用显示部分View到Controller

我有桌子

这是表格

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

我需要显示Interview_Id将为id

的行

我是来自网址的ID。我的网址就像*****。*** / Interwier / Recording / id

我为PartialView编写了Action方法

这是代码。

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

这是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");
        }
    });
});

这是PArtialView。但我觉得这个问题没用了

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

我需要的只是从网址获取ID,我该怎么做?

1 个答案:

答案 0 :(得分:1)

所以我找到了答案

以下是从网址

获取ID的代码
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);

控制器保持原样