【问题标题】:Routing in ASP.NET MVC(Linked tables)ASP.NET MVC 中的路由(链接表)
【发布时间】:2017-03-24 08:52:26
【问题描述】:

我有面试和Interwier

当我创建采访时,我需要在 View 中生成链接并将其发送给 interwier。

所以如果 Interview_id 是=5 Interviewer 将被采访, interview id=5

Table Intervier 与 Interview table 链接

这是面试官桌

CREATE TABLE [dbo].[Interwiers] (
[Interwier_id] INT            IDENTITY (1, 1) NOT NULL,
[FIO]          NVARCHAR (MAX) NULL,
[Email]        NVARCHAR (MAX) NULL,
[Telephone]    NVARCHAR (MAX) NULL,
[Birthday]     DATETIME       NOT NULL,
[City]         NVARCHAR (MAX) NULL,
[Salary]       NVARCHAR (MAX) NULL,
[English]      NVARCHAR (MAX) NULL,
[Interview_Id] INT            NULL,
[Status]       NVARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Interwier_id] ASC),
CONSTRAINT [FK_Interwiers_ToTable] FOREIGN KEY ([Interview_Id]) REFERENCES [dbo].[Interviews] ([Interview_Id]) ON DELETE CASCADE

现在我生成像 http://localhost:51542/Interwier/Welcome 这样的链接,我需要像这样的 'http://localhost:51542/Interwier/Welcome/5' 所以如果 Interwier 在 Interview_Id 的表中自动填充数据,例如 id=5。

我怎么能意识到这一点?

也许我没有清楚地写下我的问题。如果没有,不清楚的告诉我,我会编辑它

更新

这是 Interwier 的控制器,这里我将数据写入表

 public ActionResult Welcome()
    {
        return View();
    }

    // POST: Interwier/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Welcome([Bind(Include = "Id,FIO,Email,Telephone,Birthday,City,English,Salary")] Interwier interwierModel)
    {
        if (ModelState.IsValid)
        {
            db.InterwierModels.Add(interwierModel);
            db.SaveChanges();
            return RedirectToAction("WebCamCheck");
        }

        return View(interwierModel);
    }

更新2

我试着写逻辑

这是开始屏幕,我在这里创建面试(选择空缺并写下有关面试的详细信息),所以我创建了 Interwiev id。完成了。

之后我创建问题并将它们添加到面试中,然后单击下一步并将问题添加到表格中并链接到 Interview_id

这是样机

之后我需要向人们发送邀请。还有下一个样机

在 Link 我需要生成 Link to interview ,而不是像我之前写的那样。

我需要用 id 生成它。因此,用户打开它并填写有关他的信息,它将链接到此面试 ID。

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-4 routing


    【解决方案1】:

    如果您正在使用实体框架来保存数据,那么这样做很容易

    假设 Interwiers 表只有 2 列 Like Id 和 name=

    DemoEntities db = new DemoEntities();
    Interwiers  objInterwiers  = new Interwiers();
    objInterwiers.Email = "aman@gmail.com";
    db.Interwiers.Add(objInterwiers);
    db.SaveChanges();
    

    现在,如果您在表格中将其设置为自动递增,则无需在此处输入 id 字段。

    保存数据后,你可以得到你的id,比如:

    var id = objInterwiers.Id;
    

    最近添加的。

    你这个重定向你想要的

    return Redirect("/Interwier/Welcome/"+id);
    

    控制器中的整个代码如下:

    public ActionResult TestSaveData()
    {
        DemoEntities db = new DemoEntities(); // your entity name
        Interwiers  objInterwiers  = new Interwiers(); //table name
        objInterwiers.Email = aman@gmail.com;
        db.Interwiers.Add(objInterwiers);
        db.SaveChanges();
        return RedirectToAction("/Interwier/Welcome/"+id);
    }
    

    【讨论】:

    • 我已经拯救了Interwier。我需要其他,当我创建面试时,我有面试 _id。我需要生成带有面试 ID 的面试链接。将其发送给interwier,他将填写有关他的数据,并且在表中的采访ID中将是他正在通过的采访的ID。
    • 用户填写的id和你要重定向的表id一样吗?
    • 不。我进行面试,并在面试表中填写了id。然后通过 View 上的 js 生成链接。 $("#mybutton").click(function () { $("#link").val(document.location.origin+'@Url.Action("Welcome", "Interwier")'); });
    • 保存/获取 ID 后,您可以将 ID 保存到 Viewbag 等中并使用链接设置它,例如 $("#link").val(document.location.origin+'@Url.Action("Welcom‌​ e", "Interwier",@Viewbag.Id )'); });
    • 是的,我看到了您的最新更新,这是一种简单的方法,可以在创建后将 id 保留在 Viewbag 或 tempdata 中,并在您创建链接的地方使用它,就像我的上条评论一样。
    【解决方案2】:

    默认路由值设置为 {controller}/{action}/{id}。 生成链接时,您可以使用 routeValues 设置 id。 例如。如果链接是从控制器动作生成的,那么

    public ActionResult MyAction()
    {
     //code
     Return RedirectToAction("Welcome", "Interview", new{InterviewId = Interview.InterviewId});
    
    }
    

    这将导致 URL localhost:[portNumber]/Interview/Welcome/InterviewId

    【讨论】:

      猜你喜欢
      • 2011-01-06
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      相关资源
      最近更新 更多