本文介绍了单击按钮后如何从MVC 4视图将文本框值保存到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL日志表.

I have a Log table in SQL.

ID   UserName  VisitedTime  VisitedUrl IpAdress Browser

当当前用户访问任何页面时,我可以保存UserName,VisitedTime,VisitedUrl,IpAdress,Browser.

I can save UserName,VisitedTime,VisitedUrl,IpAdress,Browser when current user visit any page.

控制器

public ActionResult Index(string date1, string date2, string txt)


    {
        string browser = Request.Browser.Browser;
        string IP = HttpContext.Request.UserHostAddress;
        string userName = HttpContext.User.Identity.Name.ToString();
        string url = Request.Url.AbsoluteUri.ToString();
        mydataclass newDataclass=new mydataclass ();
        string sql = @"Insert Into Loglar (UserName,VisitedTime,VisitedUrl,IpAdress,Browser ) values 
           ('" + userName.ToString() + "','" + DateTime.Now.ToString() + "','"
               + url+ "','" + IP+ "','" + browser+ "')";

        newDataclass.DataCenterDoSql(sql);
}

班级

 public class mydataclass 
  {

 public mydataclass () { }  
 public bool DataCenterDoSql(string sql)
    {
        SqlConnection con = new SqlConnection();

      try
        {
       con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
       con.Open();
     }
        catch
        {
        }

        SqlCommand cmd = new SqlCommand();
        try
        {
            cmd.Connection = con;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        finally
        {
            con.Close();
        }
        return true;
    }


  }

正在工作.没有任何问题.

It is working.There is no any problem.

我在ViewPartial上有两个DateEdit,一个按钮,一个文本框和一个网格.当我选择两个日期时,在文本框中输入字符串并单击某些按钮的结果可以在gridview上显示.

I have two DateEdit,one button,one textbox and a grid on ViewPartial.When I select two date,enter string to textbox and click button results of some things can display on gridview.

我的问题:单击按钮后,如何将DateEdit 1,DateEdit 2和TextBox值保存到新表中.

My question : How can I save DateEdit 1, DateEdit 2 and TextBox value to my new table after click button.

新表

    ID   UserName  VisitedTime  VisitedUrl IpAdress  Browser  Date1 Date2 TextBox

推荐答案

一种方法是使用Ajax过滤数据.其他方式如下:

One way is to use Ajax to filter data. And other way is following:

在实际操作中采用以下值:

In action take that values:

public ActionResult Index(string date1, string date2, string txt)
{
   ViewBag.Date1 = date1;
   ViewBag.Date2 = date2;
   ViewBag.Txt = txt;
   ....
   //your code
}

而且在视野范围内:

@{
   string date1Value = string.Empty;
   string date2Value = string.Empty;
   string txtValue= string.Empty;

   if(ViewBag.Date1 != null) { date1Value = (string)ViewBag.Date1;  }
   if(ViewBag.Date2 != null) { date2Value = (string)ViewBag.Date2;  }
   if(ViewBag.Txt!= null) { txtValue= (string)ViewBag.Txt;  }
}

//...
<input name="date1" value="@date1Value "  />
<input name="date2" value="@date2Value "  />
<input name="txt" value="@txtValue"  />
//...

这篇关于单击按钮后如何从MVC 4视图将文本框值保存到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:11