Kasrak
Kasrak

Reputation: 1561

Getting and dealing with the Entered data in MVC - Simple

I have some questions regarding my MVC learning curve

Goal:

I want to get the txtMHM entered text after the btnStat is pushed and show the entered text via a label or span or ...

Upvotes: 1

Views: 126

Answers (2)

jim tollan
jim tollan

Reputation: 22485

Sypress,

Best practice would be to use a viewmodel for your action. However, at a very simple level, based on exactly what you have above, you could use the viewbag object to pass back the input value. without further ado, the frig, i mean code :):

Controller actions:

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

[HttpPost]
public ActionResult ChangeLabelText(FormCollection formCollection)
{
    ViewBag.LastNameEntered = formCollection["txtName"];
    return View();
}

View stuff (assumes that the view is named ChangeLabelText.cshtml of course):

@{
    ViewBag.Title = "ChangeLabelText";
}

<h2>ChangeLabelText</h2>
<form action="ChangeLabelText" method="post">
    <input id="txtMHM" type="text" name="txtName" value="" />
    <input id="btnStat" type="submit" value="Post"  />  
    <br />
    @Html.Label("Entered Text");
    <span id="spnEnteredText">@ViewBag.LastNameEntered </span>
</form>

and the above is called as such http://localhost:xxxx/Home/ChangeLabelText (where xxxx is the port number of your dev server)

I would add that this would NOT be the way that I would approach this to be honest, but is my direct response to your example. go for sharks' example using the viewmodel.

good luck

[EDIT] - I've updated my answer now that I'm at a machine, so the above should work as intended.

Upvotes: 1

user596075
user596075

Reputation:

The View Model you should use (simplified):

public class YourViewModel
{
    public string TextEntered { get ; set ; }
}

The View:

@model YourViewModel

@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.TextEntered)
    <br />
    <input id="btnStat" type="submit" value="MHM" />
}

@Html.LabelFor(m => m.TextEntered)

The Controller Action method:

[HttpPost]
public ActionResult ChangeLabelText(YourViewModel yourViewModel)
{
    return View(yourViewModel);
}



Your Altered Code

index.cshtml

@model MVCTest1.Models.EnteredTextModel 


@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(m => m.TextEntered) 
    <br /> 
    <input id="btnStat" type="submit" value="MHM" /> 
} 

@Html.LabelFor(m => m.TextEntered)

The Model

namespace MVCTest1.Models 
{ 

    public class EnteredTextModel 
    { 
        public string TextEntered { get; set; } 

    } 
} 

The HomeController

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using MVCTest1.Models ; 

namespace MVCTest1.Controllers 
{ 
    public class HomeController : Controller 
    { 
        public ActionResult Index() 
        { 
            ViewBag.Message = "Welcome to ASP.NET MVC!"; 

            return View(); 
        } 

        [HttpPost]
        public ActionResult Index(EnteredTextModel theModel) 
        { 
            return View(theModel); 
        } 

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

    } 
} 

Upvotes: 3

Related Questions