locked
how to put textbox value into the session RRS feed

  • Question

  • User-1634604574 posted

    i want to put textbox value into the session variable and call that session in all view how can i do it?

    Thursday, February 14, 2019 6:18 PM

All replies

  • User475983607 posted

    i want to put textbox value into the session variable and call that session in all view how can i do it?

    Very simple and explained several times in your previous threads.

    Index action

            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }

    Index view

    @{
        ViewBag.Title = "Index";
    }
    
    
    
    <div id="content">
        <h2>Index</h2>
        @using (Html.BeginForm("Main", "Test"))
        {
            <div>
                <input id="username" name="username" />
            </div>
            <div>
                <input id="Submit1" type="submit" value="submit" />
            </div>
    
            <div>
                @if(Session["username"] == null)
                {
                    <span>No Session Here</span>
                }
                else
                {
                    <span>Welcome @Session["username"]</span>
                }
            </div>
        }
    </div>

    Main Action

        public class TestController : Controller
        {
            public ActionResult Main(string username)
            {
                Session["username"] = username;
                return View();
            }

    Main View

    @{
        ViewBag.Title = "Main";
    }
    
    <h2>Main</h2>
    
    <div>
       Welcome @Session["username"] <br />
       Click the link below to  verify Session will persist across requests.
    </div>
    <div>
        @Html.ActionLink("Verify Session Works", "Index")
    </div>

    Please see the Getting Started with MVC tutorials.  The tutorials cover the basics like above.

    https://docs.microsoft.com/en-us/aspnet/#pivot=aspnet&panel=aspnet_tutorials

    Thursday, February 14, 2019 6:47 PM