0

This is my View

@

using (Html.BeginForm("Display", "Home", @FormMethod.Post))
    {
    <div>Name:<a>@ViewBag.st</a><br /></div>
       <a></a>
    <div>City:<a>America</a></div>
    <br />
     <input type="submit" value="Submit" /> 
    <br />
    }

THis is model class

public class User { private string m_name = string.Empty;

public string Name
{
    get
    {
        return m_name;
    }
    set
    {
        m_name = value;
    }
}

}

This is my controller class

[HttpGet]
         public ActionResult Viewdetails()
         {
            User ur=new User();
            ur.Name = "Danny";
            ViewBag.st = ur.Name;
           return View();
         }

        [HttpPost, ActionName("Viewdetails")]
        public ActionResult Test()
        {
            return View();
        }
         [HttpPost]
         public ActionResult Display()
         {
             return View();
         }
    }

Now how to display Danny in my view in my Viewdetails.cshtml instead of using

@Viewbag
Pavan
  • 61
  • 9
  • I would recommend using something like session in this case – COLD TOLD Jun 29 '12 at 05:16
  • It is not working i am getting null values @COLDTOLD – Pavan Jun 29 '12 at 06:11
  • Are you trying to do this: 1. Set `ViewBag` from `Viewdetails`, return the page you have provided. 2. Change the value in ViewBag. 3. Submit to `Display`, and Display returns a view(which you have not provided, or this is the same view as before?). 4. You again WANT TO see the value from ViewBag. Is that it? – Mohayemin Jun 29 '12 at 06:28
  • Then you got your answer, Darin has [said](http://stackoverflow.com/a/11257179/887149) exactly what I wanted to say. – Mohayemin Jun 29 '12 at 08:28

1 Answers1

4

You could use an input field so that when the form is submitted its value is sent to the Display action. Also let's clean a little bit your code and get rid of ViewBag.

So you have a model which is fine:

public class User
{
    public string Name { get; set; }
}

Then you could have a controller with 2 actions - one that populates the model and passes it to the Viewdetails.cshtml view and another action which is invoked when the form is submitted and renders the Display.cshtml view:

public class HomeController: Controller
{
    [HttpGet]
    public ActionResult Viewdetails()
    {
        User ur = new User();
        ur.Name = "SravanKumar";
        return View(ur);
    }

    [HttpPost]
    public ActionResult Display(User ur)
    {
        return View(ur);
    }
}

and then inside your Viewdetails.cshtml view you would use the model to render an input field instead of a link:

@model User
@using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
    <div>Name: @Html.EditorFor(x => x.Name)</div>
    <div>City: Secunderabad</div>
    <br />
    <input type="submit" value="Submit" /> 
}

and inside your Display.cshtml view:

@model User
<div>Thanks for choosing the name: @Model.Name</div>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • What's `View.cshtml`? I don't understand what you are talking about nor what you are trying to achieve. – Darin Dimitrov Jun 29 '12 at 07:52
  • Isn't that what I illustrated in my answer? Except that I used `Display.cshtml` instead of `View.cshtml`. If you want to use `View.cshtml` you could either rename the `Display` action or specify the view name to render as first argument when returning the view. – Darin Dimitrov Jun 29 '12 at 07:56
  • I dont want it to be displayed in a textbox instead i want to display it as a Label for which i used @Html.LabelFor(m =>m.name) For which i am getting name(property name) instead of Danny which i assigned to name @Darin Dimitrov – Pavan Jun 29 '12 at 08:41
  • If you want the value to be passed to the server you need an input field. Otherwise it will be lost. If you don't want it to be visible you could have a hidden field and a label. Then when updating the label with jQuery you will also have to update the value of the hidden field so that the name is passed when the form is submitted: `@Html.HiddenFor(x => x.Name)`. – Darin Dimitrov Jun 29 '12 at 08:45