Share via


How to bind to textbox in MVC3

Question

Friday, April 13, 2012 8:51 AM

what is the possible ways to pass data in MVC like ViewBag and ViewData?

we can bind data dropdown using viewbag and viewdata But how to bind data to textbox?

All replies (10)

Friday, April 13, 2012 9:00 AM ✅Answered

Sure.

Controller Action:

public ActionResult YourAction()
{
    //Sets your property
    string yourProperty = _db.GetProperties().FirstOrDefault();

    //Passing in "yourProperty" as the Model to the View
    return View(yourProperty);
}

//Alternativately you can bind the property to a Model that has multiple properties
public ActionResult YourAction()
{
    YourModelClass model = new Model();
    model.Property = _db.GetProperties().FirstOrDefault();
    
    return View(model);
}

Within your View (Top of your View): (The name should match the name of the Controller Action - so YourAction.cshtml / YourAction.aspx)

//You need to firstly bind your Model to a type.
@Model string  //This is your first example above. It simply binds the Model to a string

@Model YourModelClass //This binds the Model (and all of its properties) to the passed in YourModelClass

Populating your Textboxes (Again - within your View)

//Now you can access and populate a textbox in the methods previously listed

@Html.TextBox("TextBoxName",Model) //If using a string for the Model
@Html.TextBox("TextBoxName",Model.YourProperty) //If using YourModelClass as the Model

or

@Html.TextBoxFor(model => model.YourProperty) //Again - using YourModelClass

Monday, January 14, 2013 5:56 PM ✅Answered

You could perform a POST through jQuery, which would probably be the simplest option. It would look something like this (within your View): 

<script type='text/javascript'>
$(document).ready(function()
{
    //When your Search Button is clicked
    $('#yourSearchButton').click(function()
    {
         var searchTerm = $("#yourDropDown").val();
         $.post("<%Url.Action("Search","YourController"%>, { 'term' : searchterm }, function(data)
         {
             //Perform action based on return value
         });
    });
});
</script>

within your controller : 

[HttpPost]
public ActionResult Search(string term)
{
    //Perform search logic using 'term' as your search term.
}

Friday, April 13, 2012 8:53 AM

View: <%= Html.TextBox("foo", "some value") %>

Controller: ViewData["foo"]

 

 

 


Friday, April 13, 2012 8:54 AM

In MVC3 - whatever data you pass into the View through the Model should be able to populate a textbox. Here are some examples:

//This will populate a Textbox with name "NameOfTextbox" with the Model.Property property
@Html.TextBox("NameOfTextBox",Model.Property)

//This will automatically populate a textbox with a specific value from your Model
@Html.TextBoxFor(model => model.Property)

//Using ViewData it works the same
@Html.TextBox("TextboxName",ViewData["YourViewDataProperty"])

//And with ViewBag as well
@Html.TextBox("TextboxName",ViewBag.YourProperty)

Friday, April 13, 2012 8:56 AM

Can you pls give me full example, as I am getting data from database.


Friday, April 13, 2012 9:00 AM

How can we do the same with <input type="text" id = "txt1" name = "txt1" value="" />


Friday, April 13, 2012 9:01 AM

This will generate a textbox with the name "txt1":

<%= Html.TextBox("txt1") %>


Friday, April 13, 2012 9:08 AM

To generate just the textbox you selected *(without any pre-population) *- you can use mm10's suggestion of:

@Html.Textbox("txt1")

And if you want to populate with data from your Model use:

@Html.Textbox("txt1",Model) //If populating the Model with a string
@Html.Textbox("txt1",Model.YourProperty) //If using a property from your Model

Saturday, April 14, 2012 2:21 AM

Thanks a lot provideing valuable information and for yur promt reply.


Saturday, April 14, 2012 2:29 AM

I need one more help that I have a functionality as Below:

I have dropdown with values, whenever user selects a value from dropdown and clicks on Search button it fetch data from table through EF and display in the page.

I can handle the data fetch from database, But I need code to Search button functionality of take action from Search button with input value from dropdown. May be this will achive using HTTPPOST But I dont have much knowedge on how to use HTTPPOST.

Thanks in advance