Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Here is complete code   
the values of input box are not being passed to session variables

        public ActionResult Index()
        {
       
            return View("View11" );
        }

 **Index2 Code**
       below
        Here data is being not recieved  
   
  

    public ActionResult Index2()
                {
                    Session["s1"] = Request.Form["My"]; // here values are not being passed
                       Session["s2"] = Request.Form["My2"]; 
                   
                   
                    return View("View2");
                }

      View1 Code 
  html code View1
 

     <form name="form1" action="/Home/index2" method="post">
                    <input  name="My" type="text"  />
                    <input   name="My2" type="text"  />
                    <input id="Submit1" type="submit" value="submit" />
                      </form>
   

    View2 Code--->

        below
      <div> 
    @Session["s1"] 
     @Session["s2"]
    
       </div>

Can someone tell me that why Session Variable is still null
Posted
Updated 1-Jan-16 1:32am
v3
Comments
F-ES Sitecore 1-Jan-16 11:37am    
I'd recommend you get a book on MVC or go through some on-line tutorials as you're not really using MVC features that are designed to make this type of code easier. You should use model binding rather than Request.Form.

Since you're not using any model binding by the looks of it, try this:

C#
public ActionResult Index2( FormCollection o){
   ViewData["s1"] = o["My"]; 
   ViewData["s2"] = o["My2"]; 
   return View("View2");
}
 
Share this answer
 
Hello ,

The process you are following is wrong here. You must use the HTML helpers like the
C#
@Html.BeginForm Or Ajax in order to bind your model.

Please check the below link to get a better understanding on the model pass between view and Controller.
Html.BeginForm() vs Ajax.BeginForm() in MVC[^]
For pass of data between Controller and view you can use, ViewData, ViewBag Or TempData

Hope this helps,
Thanks
 
Share this answer
 
Hello,

I think you have just started your MVC development , I assume that you are curious about how MVC works .

Steps to solve your problem :-
1. Kindly decorate your Index2 Action with HttpPost attribute .

Note :-
1. Attach Debugger to see what all are the request which are send as request object .
2. Pls see that you dont have masterpage or any other page ,which has same Dom Element with same name u you have used for two input box.(My,My2).
3. Use Request.Params["My"] and check.

Although this is not best approch but for learning and experimenting MVC this is fine .
but in real time project go with MVC Model binders concept.
 
Share this answer
 

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