Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need to pass model parameters to view.
My Model class is: Machines.cs
public class Machines
        public int AutoKey;
        public string MachineGroup;
        public DateTime StartDate;
        public DateTime EndDate;
        public int Duration;
        public List<Machines> SqlAccessParameter(DateTime startDate, DateTime endDate)
            string connstr = "Data Source=USER-BILGISAYAR;Initial Catalog=Report;Integrated Security=True";
            SqlConnection myConnection = new SqlConnection(connstr);
            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();
            dataAdapter.SelectCommand = myCommand;

            DataSet dSet = new DataSet();


            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"];
            return machinePost;

My controller: MachinesController
public ActionResult MachineParameter()
         Machines model = new Machines();

         return View(model);

I need to user input (startDate and endDate) to specify date range. So in my view SqlAccessParameter(DateTime startDate, DateTime endDate) method must be used and this startDate and endDate parameters must be came from users bur I don't know how can I achieve this. Any help would be appreciated, thanks in advance.
Updated 18-Feb-13 20:28pm

1 solution

here like this line

@Html.Telerik().DateTimePickerFor(a => a.StartDate, new { @class = "input-small" })
Share this answer
pln474 19-Feb-13 8:18am    
Thanks @Asim Mahmood but it does not work. I need to acccess method with parameters which are came from user, of model in view.
Asim Mahmood 19-Feb-13 8:31am    
dear you will pass you ModelClass as parameter to method like this
public ActionResult MyMethod(MyViewModel objViewModel)

its my method on which i did work its easy dear, you'll add ActionName to button it'll automatically call this and pass object with values.
I think you are new on MVC?
pln474 19-Feb-13 9:42am    
Yes,I am very new to mvc pattern.Thanks for your help.I will try what you said.How can I add ActionName to button? To button name,id, onclick event??
Asim Mahmood 20-Feb-13 2:01am    
Ok put your Action name as your view name mean, if your view name is MachineParameter your ActionMethod name should be MachineParameter but parameter should be you ViewModel like MachineParameter(MachineViewModel objMachineViewModel) and behaviour will be [HttpPost] it will automatically call that ActionMethod. Mind it your view must be StronglyTypeView means it must have some Model.
pln474 20-Feb-13 2:32am    
I know these things but I need to use model method with parameter (here my model method is (SqlAccessParameter(Datetime startDate,Datetime endDate)))in view. startDate and endDate parameters must be came from user then according to this data which is in specific date range, I want to draw a chart which is work with json data. How can I do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900