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)
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
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.
DivakarGanta
Member
39 Points
140 Posts
How to bind to textbox in MVC3
Apr 13, 2012 12:51 PM|LINK
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?
mm10
Contributor
6455 Points
1187 Posts
Re: How to bind to textbox in MVC3
Apr 13, 2012 12:53 PM|LINK
View: <%= Html.TextBox("foo", "some value") %>
Controller: ViewData["foo"]
Rion William...
All-Star
31992 Points
5191 Posts
Re: How to bind to textbox in MVC3
Apr 13, 2012 12:54 PM|LINK
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)DivakarGanta
Member
39 Points
140 Posts
Re: How to bind to textbox in MVC3
Apr 13, 2012 12:56 PM|LINK
Can you pls give me full example, as I am getting data from database.
Rion William...
All-Star
31992 Points
5191 Posts
Re: How to bind to textbox in MVC3
Apr 13, 2012 01:00 PM|LINK
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)
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 YourModelClassDivakarGanta
Member
39 Points
140 Posts
Re: How to bind to textbox in MVC3
Apr 13, 2012 01:00 PM|LINK
How can we do the same with <input type="text" id = "txt1" name = "txt1" value="" />
mm10
Contributor
6455 Points
1187 Posts
Re: How to bind to textbox in MVC3
Apr 13, 2012 01:01 PM|LINK
This will generate a textbox with the name "txt1":
<%= Html.TextBox("txt1") %>
Rion William...
All-Star
31992 Points
5191 Posts
Re: How to bind to textbox in MVC3
Apr 13, 2012 01:08 PM|LINK
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 ModelDivakarGanta
Member
39 Points
140 Posts
Re: How to bind to textbox in MVC3
Apr 14, 2012 06:21 AM|LINK
Thanks a lot provideing valuable information and for yur promt reply.
DivakarGanta
Member
39 Points
140 Posts
Re: How to bind to textbox in MVC3
Apr 14, 2012 06:29 AM|LINK
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