Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get some user entered values from a view to the controller to filter the data that will be displayed. I have managed to get the view working as far as accepting the entries and then going back to the controller when the submit button is pressed but I can't seem to figure out a way to get the values from the textboxes of the view. I totally admit, I'm a complete noob when it comes to MVC outside the standard. Code posted as well. What can I do to get at the values in the textboxes to the controller on post?

Thanks

What I have tried:

UserSelection View:

C#
@using (Html.BeginForm())
{
<div class="input-group date" data-provide="datepicker" id="txtbxStart">
    <input type="text" class="form-control">
    <div class="input-group-addon">
        <span class="glyphicon glyphicon-th"></span>
    </div>
</div>

<div class="input-group date" data-provide="datepicker" id="txtbxEnd">
    <input type="text" class="form-control">
    <div class="input-group-addon">
        <span class="glyphicon glyphicon-th"></span>
    </div>
</div>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Submit" class="btn btn-default" />
    </div>
</div>
}


Controller:

C#
public ActionResult UserSelection()
{
    return View();
}
[HttpPost]
public ActionResult UserSelection(FormCollection form)
{
    TempData["dtStart"] = form["txtbxStart"].ToString();
    TempData["dtEnd"] = form["txtbxEnd"].ToString();

    return Redirect("Index");
}
Posted
Updated 12-Apr-17 17:26pm

1 solution

use name attribute in the HTML elements to access the value from view to the controller
<input name="txtbxStart" type="text" class="form-control">

<input name="txtbxEnd" type="text" class="form-control">

Controller
string start  = form["txtbxStart"].ToString();
         string end = form["txtbxEnd"].ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900