Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript MVC
Hi!
I need to pass model parameters to view.
My Model class is: Machines.cs
public class Machines
    {
        [Key]
        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);
            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;
        }
        
    
    }
 

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.
Posted 18-Feb-13 4:43am
pln474273
Edited 18-Feb-13 20:28pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

here like this line
 
@Html.Telerik().DateTimePickerFor(a => a.StartDate, new { @class = "input-small" })
  Permalink  
Comments
pln474 at 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 at 19-Feb-13 8:31am
   
dear you will pass you ModelClass as parameter to method like this
[HttpPost]
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 at 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 at 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 at 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?
Asim Mahmood at 20-Feb-13 5:13am
   
then try with jQuery
follow this link it's calling server side method passing parameters and you can perform your functionality.
http://stackoverflow.com/questions/5046930/jquery-send-string-as-post-parameter
pln474 at 21-Feb-13 3:43am
   
@Asim Mahmood Thanks for your reply, it helped me so much to fix my problem.Thank you again!
Asim Mahmood at 21-Feb-13 8:16am
   
you welcome but you didn't give me even one point :(:( ;)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100