【问题标题】:Select row in table via AJAX and display it (ASP.NET MVC)通过 AJAX 选择表中的行并显示它(ASP.NET MVC)
【发布时间】:2017-03-28 07:45:37
【问题描述】:

我有 div 我需要通过 AJAX 调用 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 是来自 url 的 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");
        }
    });
});

这里是部分视图。但我认为在这个问题上没用

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

我只需要从 url 获取 id,我该怎么做?

【问题讨论】:

  • 我在其中一个问题中解决此问题的方法是在“public ActionResult Recording(int id)”方法中将 Id 设置为会话变量或全局变量,并使用该变量“QuestionBlock”跨度>
  • 我找到了通过 JS 从 url 获取 id 的方法。看起来像这样var url = window.location.pathname; var id = url.substring(url.lastIndexOf('/' + 1));。现在将检查它是否按我预期的那样工作。 @devil_coder
  • 干得好@E.S 谢谢
  • 看,我的代码都在 localhost:5654 之后。现在将编写代码以仅获取 id @devil_coder
  • 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); 此代码完美运行。 @devil_coder

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


【解决方案1】:

所以我找到了答案

这是从 url 获取 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);

控制器保持原样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2020-04-18
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    相关资源
    最近更新 更多