从URL获取ID并写入数据表

时间:2017-03-26 19:43:35

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我有Interwier表

这是表格代码

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

);

以下是我的观看方式

My View

我有这样的链接http://localhost:51542/Interwier/Welcome/2093,其中2093是Interview_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);
    }

我需要从链接到表格写入id。我怎么能这样做?

更新

我尝试用ViewBag解决问题并面对问题

值不写入Interview_Id

HiddenFor不为null enter image description here

为什么这样?为什么不写作?

更新

问题解决了

@Shyju方法有效。谢谢老兄

2 个答案:

答案 0 :(得分:0)

您需要通过视图(表单)传递它。

下面的示例使用ViewBag字典将数据从操作方法传递到视图。如果您有一个视图模型,请为此访问ID添加一个属性,并使用该属性将数据传递给视图(并在提交表单时返回到httppost操作方法

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;


    public class RainBow2{
        static int x,y,z;
        static RainBowPanel rainBow = new RainBowPanel();
        static StdRainBow stdRainBow = new StdRainBow();
        static JTextField xCo = new JTextField(4);
        static JTextField yCo = new JTextField(4);
        static JTextField angle = new JTextField(4);
        static JFrame frame;

        public static void main(String[] args){
            Color color = new Color(135,206,250);
            frame = new JFrame("Rainbow");
            JPanel panel = new JPanel();
            JButton draw = new JButton("Draw RainBow");
            draw.addActionListener(new ListenButton());
            JLabel lab1 = new JLabel("X-Coordinate");
            JLabel spaceLab = new JLabel("    ");
            JLabel lab2 = new JLabel("Y-Coordinate");
            JLabel angleLab = new JLabel("Initial Angle");
            JButton chButton = new JButton("Change Color");
            chButton.addActionListener(new ListenButton());
            panel.setBackground(color);
            panel.add(angleLab);
            panel.add(angle);
            panel.add(lab1);
            panel.add(xCo);
            panel.add(spaceLab);
            panel.add(lab2);
            panel.add(yCo);
            panel.add(draw);
            panel.add(spaceLab);
            panel.add(chButton);
            frame.getContentPane().add(BorderLayout.SOUTH,panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(1366,740);
            frame.setVisible(true);
            rainBow.addMouseListener(new RainBowList());
            frame.getContentPane().add(rainBow);


        }

        static class RainBowList extends MouseAdapter implements MouseMotionListener{
            public void mouseClicked(MouseEvent e){
                x = e.getX();
                y = e.getY();
                rainBow.drawing(x, y, 0);

            }

        }

        static class ListenButton implements ActionListener{
            public void actionPerformed(ActionEvent a){
                if(a.getActionCommand().equals("Draw RainBow")){
                    x = Integer.parseInt(xCo.getText());
                    y = Integer.parseInt(yCo.getText());
                    z = Integer.parseInt(angle.getText());
                    rainBow.drawing(x, y,z);
                }
                if(a.getActionCommand().equals("Change Color")){
                    rainBow.drawing(x, y,z);
                }
                            }

        }




import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class RainBowPanel extends JPanel{

    int x,y,z;

    public void drawing(int xx, int yy, int zz){
        Color color = new Color(135,206,250);
        setBackground(color);
        z = zz;
        x = xx;
        y = yy;
        repaint();
    }


    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int length = 300;
        int width = 300;
        x = x-length/2;
        y = y-width/2;

        for(int i =0 ; i< 7;i++){
            Color color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
            g.setColor(color);
            g.fillArc(x,y,length ,width ,z,180 );

            x=x+15;
            y=y+15;
            length = (length-30);
            width = (width-30);
            try{
                Thread.sleep(200);
                }catch(Exception e){

                }
        }

        }

}





    }

现在在您的视图中,将此值保留在表单内的隐藏字段中。

public ActionResult Welcome(int id)
{
   ViewBag.InterviewId=id;
   return View();
} 

并将<input type="hidden" name="Interview_Id" value="@ViewBag.InterviewId" /> 属性包含在Interview_Id属性Bind列表中提交表单时,将在该属性中提供interviewId值。

Include

答案 1 :(得分:0)

您需要将id传递给视图并将此ID添加到隐藏的属性Interview_Id字段中,这样您的操作就会像这样

public ActionResult Welcome(int id)
{
   ViewBag.Id=id;
   return View();
} 

并在视图中添加隐藏字段的值

 @Html.HiddenFor(x=> x.Interview_Id,new {@Value = ViewBag.Id })

在操作中,它将添加到模型中并添加Interview_Id

[Bind(Include = "Interview_Id,Id,FIO,Email,Telephone,Birthday,City,English,Salary")