C# 从URL获取ID并写入数据表
我有一张桌子 这是表格代码C# 从URL获取ID并写入数据表,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我有一张桌子 这是表格代码 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
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/2093
其中2093是面试Id
现在,我写入datatable的操作如下所示
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);
}
public ActionResult Welcome(int id)
{
ViewBag.Id=id;
return View();
}
我需要将id从链接写入表。我怎么能做到
更新
我尝试用ViewBag和face解决问题
值未写入到访谈\u Id
HiddenFor不为空
为什么会这样?为什么不写呢
更新
问题解决了
@Shyju方法有效。Thank’s dude您需要通过视图(表单)传递它 下面的示例使用ViewBag dictionary将数据从action方法传递到视图。如果您有一个视图模型,请为此访谈id添加一个属性,并使用该属性将数据传递到视图(并在提交表单时返回到httppost操作方法) 现在,在您的视图中,将此值保留在窗体内的隐藏字段中
<input type="hidden" name="Interview_Id" value="@ViewBag.InterviewId" />
您需要将
id
传递到视图,并将此id添加到属性的隐藏字段Interview\u id
,这样您的操作将如下所示
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);
}
public ActionResult Welcome(int id)
{
ViewBag.Id=id;
return View();
}
并在视图中添加隐藏字段的值
@Html.HiddenFor(x=> x.Interview_Id,new {@Value = ViewBag.Id })
实际上,它将被添加到模型中,并添加Interview\u Id
[Bind(Include = "Interview_Id,Id,FIO,Email,Telephone,Birthday,City,English,Salary")
我确实喜欢这样,但面试Id在中为空table@E.S我的坏资产价值为V