1
votes

I am new to MVC.Basically I need to pass values entered in the textbox from my view to controller action method. As I enter the values in the text box and click the enter button I need to display the value on the screen. I am currently unable to do so. Please find my code below The model class

 public class ProteinTrackingService
    {
        public int? Total { get; set; }
        public int Goal { get; set; }

        public void AddProtein(int? amount)
        {
            Total += amount;
        }

    }

The controller class

 public class ProteinTrackerController : Controller
    {

        ProteinTrackingService proteinTrackingService = new ProteinTrackingService();
        // GET: ProteinTracker
        public ActionResult Index()
        {
            ViewBag.Total = proteinTrackingService.Total;
            ViewBag.Goal = proteinTrackingService.Goal;
            return View();
        }

        // GET: ProteinTracker/Details/5

        public ActionResult AddProtein(ProteinTrackingService model)
        {
            proteinTrackingService.AddProtein(model.Total);
            ViewBag.Total = proteinTrackingService.Total;
            ViewBag.Goal = proteinTrackingService.Goal;

            return View("Index");
        }


    }

The view

using (Html.BeginForm("ProteinTracker", "AddProtein",FormMethod.Post))
{
    @Html.AntiForgeryToken()



        <form>
            <div class="form-horizontal">
                <h4>Protein Tracker</h4>
                <hr />

                Total : @ViewBag.Total
                Goal  : @ViewBag.Goal

                <input id="Text1" type="text" value="TextInput" />  <input type="Submit" value="Add"  />



            </div>
        </form>

}

I am modifying the code above based on your suggestions. I basically need to display the following in the view

Total : value Goal : value

Textbox control (To enter the total) Button (pass the total to contoller) Please note that when the user clicks the Add button the total should show in above field Total : value.

New View

@using (Html.BeginForm( "AddProtein","ProteinTracker", FormMethod.Post))
{
    @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>Protein Tracker</h4>
                <hr />

                 @Html.LabelFor(m => m.Total, "Total" )  <hr />
                 @Html.LabelFor(m => m.Goal, "Goal")   <hr />
                 @Html.TextBoxFor(m => m.Total)   <hr />

                 <input type="Submit" value="Add"  />
            </div>

}

New Controller

public class ProteinTrackerController : Controller {

ProteinTrackingService proteinTrackingService = new ProteinTrackingService();
// GET: ProteinTracker
public ActionResult Index()
{
    var model = new ProteinTrackingService()
    { Total = proteinTrackingService.Total, Goal = proteinTrackingService.Goal };
    return View(model);

}

// GET: ProteinTracker/Details/5

public ActionResult AddProtein(ProteinTrackingService model)
{
    proteinTrackingService.AddProtein(model.Total);
    model.Total = proteinTrackingService.Total;
    model.Goal = proteinTrackingService.Goal;

    return View("Index",model);
}

}

2
Do not use ViewBag to pass values to your view - initialize your model and set its properties and pass the model to the view. And in the view, bind to your model using @Html.TextBoxFor(m => m.Total) etc. Your manual html does not even have a name attribute so will not submit anything. Suggest you go to the MVC site and work through the tutorials to understand the basicsuser3559349
You also have invalid html (nested forms) - remove the inner <form> tags.user3559349
How do i initialise and pass the model to the view. Do I need to do it in the index methodTom
var model = new ProteinTrackingService() { Total = x, Goal = y }; return View(model);user3559349
Hi I have modified the code above and having issues diplaying the value in the viewTom

2 Answers

0
votes

You need to add the HttpPost attribute to your action.Looking at your form @using (Html.BeginForm( "AddProtein","ProteinTracker", FormMethod.Post)) , apparently you are sending a post request to your controller.

[HttpPost]
public ActionResult AddProtein(ProteinTrackingService model)
{
    proteinTrackingService.AddProtein(model.Total);
    model.Total = proteinTrackingService.Total;
    model.Goal = proteinTrackingService.Goal;

    return View("Index",model);
}
-1
votes

First of all your this syntax

using (Html.BeginForm("ProteinTracker", "AddProtein", FormMethod.Post))

already creates a form tag when html generates. No need to create from tag again in it.

So for your want, in view you need give to your input field a name

 <input id="Text1" type="text" value="TextInput" name="textname"/>

and add this name as parameter in your controller method like that

public ActionResult AddProtein(ProteinTrackingService model,string textname)
{
    // your code
    return View("Index");
}

It will pass your textfield value from view to controller. For clearing your concept you may visit Loopcoder.com