在url中传递用户输入

时间:2013-02-15 14:12:36

标签: asp.net-mvc

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

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

            return View();
    }

我在视图中从用户那里得到了开始和结束时间。 我希望看到这样的网址:Course / Parameter / start = userinput&&端= 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;
    }}

2 个答案:

答案 0 :(得分: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 
                           }, 
                       )) 

这将为您提供如下所示的网址:

Course/Parameter?start=userinput&end=userinput

答案 1 :(得分:0)

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

如果您使用的是jQuery ajax,请通过将data设置为:

来传入
{
    start: value,
    end: value
}

并将dataType设为“json”。

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