2013-02-15 107 views
0

我需要将用户输入传递给url。 我CourseController操作是:在url中传递用户输入

public ActionResult Parameter(DateTime start, DateTime end) 
    { 
     //some operations 

      return View(); 
    } 

我从在我看来,用户得到的开始和结束时间。 我想看到这样的网址:课程/参数/开始=用户输入& &结束= userinput 任何帮助,将不胜感激。

My model is: 
     public class MachineSql{ 

     public List<Machines> SqlAccessParameter(DateTime startDate, DateTime endDate) 
    { 

     SqlConnection myConnection = new SqlConnection(connstr); 
     myConnection.Open(); 
     SqlCommand myCommand = new SqlCommand("DateRange",myConnection); 
     myCommand.CommandType = CommandType.StoredProcedure; 
     myCommand.Parameters.Add("@SP_startDate", SqlDbType.DateTime).Value = startDate; 
     myCommand.Parameters.Add("@SP_endDate", SqlDbType.DateTime).Value = endDate; 

     SqlDataAdapter dataAdapter = new SqlDataAdapter(); 
     myCommand.ExecuteNonQuery(); 
     dataAdapter.SelectCommand = myCommand; 

     DataSet dSet = new DataSet(); 
     dataAdapter.Fill(dSet); 

     myConnection.Close(); 

     List<Machines> machinePost = new List<Machines>(); 
     foreach (DataRow row in dSet.Tables[0].Rows) 
     { 
      Machines mac = new Machines(); 
      mac.AutoKey = (int)row["AUTOKEY"]; 
      mac.MachineGroup = (string)row["MACHINEGROUP"]; 
      mac.Duration = (int)row["DURATION"]; 
      mac.StartDate = (DateTime)row["STARTTIME"]; 
      mac.EndDate = (DateTime)row["ENDTIME"]; 
      machinePost.Add(mac); 
     } 
     return machinePost; 
    }} 
+1

当你说“我从用户的角度看我的开始和结束”时,你的意思是使用表单提交它吗? – tomasmcguinness 2013-02-15 14:14:01

+0

@tomasmcguinness是的,它使用这样的表单提交:@using(ajax.beginform('ActionName','ControllerName')) – pln 2013-02-15 14:41:01

+0

为什么你想要通过URL传递的值? – tomasmcguinness 2013-02-15 14:48:35

回答

0

由于您使用Ajax的助手很容易添加URL参数:

@using(ajax.beginform('Parameter', 
         'Course', 
         //here is how you add url params 
         new { 
          start = @Model.StartDate, 
          end = @Model.EndDate 
          } 
         // Any ajax actions needed such as HyypMethod, 
         // OnSuccess, etc... 
         new AjaxOptions 
          { 
           //options here 
          }, 
         )) 

这会给你看起来像一个URL:

Course/Parameter?start=userinput&end=userinput 
0

只要确保你将两个字段放在名为startend的表单中,并且它们应该是提交给该控制器方法的表单的一部分。当路由匹配到该控制器方法时,ASP.NET MVC将自动将值转换为DateTime。

如果您正在使用jQuery的阿贾克斯,然后通过设置data传中:

{ 
    start: value, 
    end: value 
} 

并设置dataType为 “JSON”。

有关详细信息,请参阅http://api.jquery.com/jQuery.ajax/

相关问题