0

I am entirely new to asp.net mvc and this is my first sample project that I need to show one textbox in one view when the user entered a value in that textbox, I need to display that value in label in another view. for that I have done like this ..

this is my controller class

public class TextBoxController : Controller
{
  //
  // GET: /TextBox/

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

and this is my model class

namespace MvcTestApplication.Models
{
  public class TextboxModel
  {
    [Required]
    [Display(Name= "Textbox1")]
    public string EnteredValue { get; set; }
  } 
}

and this is my view

@model MvcTestApplication.Models.TextboxModel

@{
  ViewBag.Title = "TextboxView";
}

<h2>TextboxView</h2>
@using (Html.BeginForm())
{
   <div>
   <fieldset>
       <legend>Enter Textbox Value</legend>
       <div class ="editor-label">
       @Html.LabelFor(m => m.EnteredValue)
       </div>
       <div class="editor-field">
           @Html.TextBoxFor(m=>m.EnteredValue)
       </div>
       <p>
            <input type="submit" value="Submit Value" />
        </p>
     </fieldset>
   </div>
 }

I am not able to see any textbox and any button on page and I am getting error like

HTTP:404 : Resource Cannot be found

I am using visual studio 2012 and mvc4..

would any pls suggest any idea on this one .. Many thanks..

6
  • 1
    What URL are you using? Commented Jul 24, 2013 at 8:42
  • i am just getting url like this.. localhost:2234 Commented Jul 24, 2013 at 8:43
  • 2
    Try localhost:2234/TextBox (or look at your route config) Commented Jul 24, 2013 at 8:43
  • Try this url localhost:2234/TextBox/Index Commented Jul 24, 2013 at 8:45
  • Try this rename TextBoxController with some other name, because this is reserve word Commented Jul 24, 2013 at 8:46

2 Answers 2

1

RE-WRITTEN

In simple terms, to access a page on ASP.NET MVC, you should point the URL to its controller name. In this case, TextBox:

localhost:2234/TextBox/TextBox

Also, you forgot to add an ActionResult for this new view. When you're loading the page, it's going through the Index one, which is empty.

The final code should look like this:

Controller

public class TextBoxController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult TextBox(MvcApplication1.Models.TextBoxModel model)
    {
        return View(model);
    }

}

Model

public class TextBoxModel
{
    [Required]
    [Display(Name = "Textbox1")]
    public string EnteredValue { get; set; }
}

Razor View (Index)

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

Razor View (TextBox)

@model MvcApplication1.Models.TextBoxModel

@{
    ViewBag.Title = "TextBox";
}

<h2>TextBox</h2>
@using (Html.BeginForm())
{
<div>
    <fieldset>
        <legend>Enter Textbox Value</legend>
        <div class ="editor-label">
            @Html.LabelFor(m => m.EnteredValue)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m=>m.EnteredValue)
        </div>
        <p>
            <input type="submit" value="Submit Value" />
        </p>
    </fieldset>
</div>
 }
Sign up to request clarification or add additional context in comments.

7 Comments

I have tried this one localhost:2234/TextBox ... I am getting empty page in that i am able to see this word "Index"....not able to see textbox and button
Yes, sorry about that. Just seen it.
What if you try localhost:2234/TextBox/Index?
i am getting same word "Index" on the screen ... even if i typed this one localhost:2234/TextBox/Index
That's really weird. Let's go through the project structure/files then. Tell me the file names of your controller, model and view. Also, on which folders they are placed.
|
0

Make sure that you have registered the URL via route config.

Find more about asp.net routing here

UPDATE:

Make sure that file name of your view is Index.cshtml since your controller doesn't specify any return view names.

1 Comment

If there's no different parameters than the usual, you don't need to change the routes.

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.