2013-02-15 57 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/