Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two textbox on same view on project when I first time load we provide value on textbox from controller using model.name its show in view than i click on button another textbox show other value model.age but the model.name value is remove I want both the values in textbox

What I have tried:

<pre>namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home

        Class1 cs = new Class1();
        public ActionResult Index()
        {
            if(TempData["A"]==null)
            {
                
                cs.name = "hi";
                TempData["A"] = "B";
            }
            
            return View(cs);
        }

        public ActionResult A()
        {
           cs.age ="hello";
            //return RedirectToAction("Index",cs);
            return View("Index", cs);

        }


        }
}
Posted
Updated 5-Aug-18 2:52am
Comments
F-ES Sitecore 3-Aug-18 6:46am    
You have to make sure the button you click is submitting a form and you add the model to the action. So if your button submits to "A";

public ActionResult A(Class1 model)

If you have properly bound your textbox to Model.name in the view then "model" in "A" will have that data also. Without seeing your view it's impossible to answer.

You really need to get a better basic understanding of MVC though, I suggest you go through a book or some tutorials like the "MVC Music Store".

I am sure about your view : -
you have to bind back your model on each request to preserve its value.
Your code would be something like:-

Home Controller

    public class HomeController : Controller
    {       
        Class1 cs = new Class1();
        [HttpGet]
        public ActionResult Index(Class1 model)
        {
            cs.name = "hi";
            if (TempData["A"] == null)
            {
                TempData["A"] = "B";
            }

            return View(cs);
        }
       
        public ActionResult A(Class1 model)
        {
            model.age = "hello";
            return RedirectToAction("Index", model);
            //return View("Index", cs);

        }      
    }

Index View

    <pre>@using Articles.Models
    @model Class1
    @{
       Layout = null;
    }

    <!DOCTYPE html>
 
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        @using (Html.BeginForm("A", "Home", FormMethod.Post))
        {
            <div>
                @Html.LabelFor(model => model.name)   
                @Html.EditorFor(model => model.name)
            
                <br />
                @Html.LabelFor(model => model.age)     
                @Html.EditorFor(model => model.age)
            </div>
            <div class="options">
               <input type="submit" name="save" class="btn btn-success" value="Save" />
            </div>
        }
    </body>
    </html>
 
Share this answer
 
v2
You need to switch to a model driven UI. Both ends of your UI can manipulate the model and fetch data from it to display it.

=> Create a data model class and handle its changes and display in the UI
 
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