0

I am trying return a string from controller to the same view in MVC

but its returning to a new view

Below is the code of my view:

@using (Html.BeginForm("Index2", "home", FormMethod.Post))
{

<label></label>    
 <input type="submit" value="Click Me" />
}

Below is the code of my controller:

  public ActionResult Index2()
    {
        string _return = "Hello world";
        return Content(_return);

    }

how can I display "hello world" in the label

thanks.....

1
  • Looks like Razor syntax for ASP.NET MVC with c# code. Please note that the model-view-controler tag specifically states to use asp.net-mvc tag for ASP.NET MVC questions. Commented Sep 23, 2015 at 14:52

2 Answers 2

2
You can pass the value using Model objects or simply use ViewBag

public ActionResult Index2()
{
    ViewBag.return = "Hello world";
    return View("YourViewName");

}

In View

<label>@ViewBag.return</label>   
Sign up to request clarification or add additional context in comments.

6 Comments

thank you for your response ....how to this after clicking button.one question : how do i return partial view in the same view after clicking button can u pls give me examples
its will return in new view but i want in same view
Partial view is nothing but a part of your view/page. It loads in same view. Above link is just an example to load PV.
thank you ...can u address me for tutorial with coding
|
1

Make the model type of your view string like this:

@model string

And use that string in your code like this:

The action told me to say @Model

Another way, if you want to display the content of an action method is to call Html.Action in your view:

@Html.Action("Index2")

1 Comment

i have tried your code but still its showing in new [email protected]("Index2") is showing before clicking button ....where i making error...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.